-
Notifications
You must be signed in to change notification settings - Fork 416
Questions about Concurrent Ruby #379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
When you want to run, say, 500 blocking operations at once (I/O Server), do you still need 500 actors? |
@Asmod4n The best person to answer your question is @pitr-ch since he built our actor implementation, but the short answer is Yes. But this also raises the question, is actor the best abstraction for your problem? Like all our high-level abstractions, our actors run on one of the global thread pools. (You can also use dependency injection to provide a custom thread pool if you like.) Internally, however, each actor uses a class called If you want to run many I/O tasks simultaneously, we have other high-level abstractions that may be suitable for the task. A set of require 'concurrent'
stock_symbols = [...] # provide a list of stock symbols here
futures = symbols.collect do |stock_symbol|
Concurrent::Future.execute { FinanciApi.get_stock_price(stock_symbol) } # do the I/O operation here
end
stock_prices = futures.collect {|future| future.value } # iterate over all futures and get the values The above code will run all the futures on the This highlights one of the biggest advantages of |
@Asmod4n On point of clarification, it's probably not a good idea to run 500 I/O operations concurrently. There are performance costs associated with creating and managing a large number of threads. This is a situation where our ability to inject a custom executor is very handy: pool = Concurrent::FixedThreadPool.new(10)
futures = symbols.collect do |stock_symbol|
Concurrent::Future.execute(executor: pool) { FinanciApi.get_stock_price(stock_symbol) }
end |
@jdantonio thats what i was looking for :) |
Another approach (it would be interesting to compare the performance) could be to use some event loop lib (e.g. EventMachine) to deal with the IO, doing only necessary work in callbacks, distributing rest of the work to actors, futures, etc. |
Call be uninformed, but doesn't all I/O have to be handled by EventMachine for the current Process then? |
It might be better to do it all uniformly on EventMachine but it's not required. E.g. all http requests could go through EM where translated to messages and then to actors for processing, but other parts of the code can use IO blocking operations to access files or other stuff without EM. |
EventMachine suggests that all concurrency be handled by EM, but that's not a requirement. EM just uses Ruby threads under-the-hood so you can mix-and-match. You just have to know what you are doing. The flow @pitr-ch suggests should work fine. The basic application flow would be:
|
With the Riding Rails announcement last week and this discussion over at ActionCable, there are a lot of people talking about concurrent-ruby. Please use this thread for general questions about who we are, what we do, and what we provide.
The text was updated successfully, but these errors were encountered: