EventMachine executes all operations in separate threads and all callbacks of this operations in one mainthread. It's very useful when you need reduce competitiveness in data processing between portions of data. You can recieve data from network concurent and process results in queue. But I need go even futher. I have many users on my site and want to make separate queue for each of them. So it should behave like single EM for each user. But I can't. EventMachine is singletone.
Thread.new do
EM.run
end
def request_from_user
operation = Proc.new {
# network request
}
callback = Proc.new {
# some heavy data processing
2*2
}
5.times do
EM.defer(operation, callback)
end
end
3.times do
request_from_user
end
So we have callback-mess in mainthread queue. Some data will be processed in end of queue, some in the middle and it's awful. Heavy load will enlarge the queue and some users will recieve results with very big delay. If I run request_from_user in single thread callbacks will be executed in one EM main thread anyway.
So what can I do with it? Is it possible to create separate callbacks queue for each user and execute it concurent or not? May be even without EventMachine. Celluloid, whatever...