reactor / async-IO with concurrent-ruby

89 views
Skip to first unread message

Arindam Mukherjee

unread,
Jul 22, 2016, 4:55:24 PM7/22/16
to Concurrent Ruby
I am a concurrent-ruby noob but find immediate value in this library for use in my work. I had been investigating reactor-models for doing async-IO and was looking EventMachine. But my requirements go well-beyond just async-IO. In a peanut-shell, I need to run I/O bound tasks that may sometimes do CPU-bound stuff. They are workflow like, so something like Concurrent::Future.dataflow seems very useful.

With that said, are there any recommendations on how to optimally do I/O using concurrent-ruby.

1. Should I just create futures that do blocking I/O and rely on the internal threadpool to schedule these operations?
2. Are there any async I/O libraries for different protocols that are suggested for use with concurrent-ruby?
3. Is there some way to use Fibers in conjunction with concurrent-ruby or is it mostly not needed?

Thanks,
Arindam

Chris Seaton

unread,
Jul 22, 2016, 5:00:23 PM7/22/16
to Arindam Mukherjee, Concurrent Ruby
Do you know ahead of time if a task will be IO-bound or CPU-bound?

If you do, create two thread pools. One has n threads for n cores on your machine, and is used to run all CPU-bound tasks. The other has maybe j threads to run all IO-bound tasks. I’m not sure how big you would make j. You’d have to experiment with say 10, 100 or 1000 threads and see how things work.

In this arrangement you won’t run more CPU-bound tasks than you have cores for, and IO-bound tasks never block a thread that could be used for CPU-bound tasks.

Chris

--
You received this message because you are subscribed to the Google Groups "Concurrent Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email to concurrent-ru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alastair Pharo

unread,
Jul 23, 2016, 4:04:37 AM7/23/16
to Arindam Mukherjee, Concurrent Ruby

Hey there,

Firstly, I would agree with those that say that async IO isn’t necessary with concurrent-ruby in a lot of cases. In my experience, it becomes useful when you need to have a large number of file handles open at once and having a thread for every one is too expensive.

I have an unfinished but usable library that can be do async socket IO with concurrent-ruby. It is here: https://github.com/asppsa/concurrent_io. There’s no up-to-date gem at the moment, although the code is being used in a production environment to run a websocket server. I created the gem because I ran into resource problems like I mentioned above. The README is hopelessly outdated, so in brief …

  • The project wraps various async IO ruby frameworks (the main one being eventmachine, but there is also plain ruby support using IO#select, and some support for nio4r).

  • In all cases, it runs a single “selector” thread that watches a set of IO objects for availability to read/write or for close/error events, and then notifies an object.

  • The main use case at the moment is to give the CR Actor framework a means of passing messages across sockets, but its not limited to using actors. If your use case doesn’t involve sockets though, I’m not sure how well it will work.

  • You can check out the examples folder to see a basic example of how to do ping-pong over a socket.

If you’re interested in using this at all I can endeavour to clean it up a bit and make a release. Another possibility is you could look into the Rails ActionCable codebase, as I understand that it uses CR and does async IO somehow. Part of the reason that I’ve not bothered to release concurrent_io is that I somewhat expected it to be superceded by the work of the rails team.

Cheers,

Alastair

On 23 July 2016 at 06:55, Arindam Mukherjee wrote:

I am a concurrent-ruby noob but find immediate value in this library for use in my work. I had been investigating reactor-models for doing async-IO and was looking EventMachine. But my requirements go well-beyond just async-IO. In a peanut-shell, I need to run I/O bound tasks that may sometimes do CPU-bound stuff. They are workflow like, so something like Concurrent::Future.dataflow seems very useful.

With that said, are there any recommendations on how to optimally do I/O using concurrent-ruby.

  1. Should I just create futures that do blocking I/O and rely on the internal threadpool to schedule these operations?
  1. Are there any async I/O libraries for different protocols that are suggested for use with concurrent-ruby?
  1. Is there some way to use Fibers in conjunction with concurrent-ruby or is it mostly not needed?

    Thanks,
    Arindam


    Arindam Mukherjee

    unread,
    Jul 24, 2016, 5:52:06 AM7/24/16
    to Concurrent Ruby, arindam....@gmail.com
    Concurrent IO does seem interesting to me although I wouldn't immediately start using it. I will get back to you with more questions as I experiment with it.

    Thanks,
    Arindam
    Reply all
    Reply to author
    Forward
    0 new messages