I’d like to add a few notes if you don’t mind.

“Processes are slow to create and destroy.”

I’m not sure about this. At least in Linux, threads are implemented as processes, and both are pretty fast to create and destroy. I’d love to get some explanation on why processes would be slow(er) to create and destroy when compared to threads. Do you have any links/benchmarks backing that statement?

“One common reason to use EventMachine is the case when you have a lot of I/O operations and you don’t want to deal with threads manually. Manually handling threads can be difficult or often too expensive from a resource usage point of view.”

I suspect that the main reason why EventMachine exists is actually the “too expensive from a resource usage point of view” reason. I don’t think it’s much related to dealing with threads manually being hard. Without the select() based approach, the usual implementation for handling multiple connections is to allocate a thread per connection. With persistent connections this becomes an issue when there are tons of them. Not only a lot of RAM would be needed to handle too many threads/processes but there are sometimes limits to the number of threads allowed by the OS that could potentially limit a lot the number of allowed concurrent persistent connections. Using select() and similar approaches (like the NIO libraries for Java based servers) is the solution used by EventMachine to overcome this problem, by listening to events like data transferred to one of the connections before taking any actions. This way there’s no need for multiple threads or processes when the OS provides smarter support for async I/O.

)

    Rodrigo Rosenfeld Rosas

    Written by