Reputation:
I made a website using ruby on rails. Now, I need to write a chat/game in flash/action script, and I decided that the game server would be in ruby (mainly, because I have my AR models already, it's a language I know, and because the chat/game is mostly IO bound, it's not computation heavy). At first I thought I would go with a multithreaded apps (running with jruby), a thread for each client. Then I realized that it would be a bad idea. I then thought about using select/kqueue/epoll. I did my research on if there was a library such as twisted (for python) but for ruby, and turns out there is EventMachine (which does more or less the same thing). I have a hard time wrapping my head around the evented/callbacky way of doing things, but I'll be alright I guess. There's one thing though: I realized that using ActiveRecord meant that my database queries would block, which is a bad thing. I looked up "asynchronous ActiveRecord" online and found some interesting things (asynchrony, neverblock, em_postgresql, etc.) but I still don't know how to apply them. If I understand correctly, asynchrony is basically EM with fibers, which would allow me to write evented code that looks like normal code (could someone explain this to me?). Em_postgresql is (I think) a postgresql driver that uses fibers to work along with asynchrony, I believe. Is that correct?
I'm quite lost and I'd like someone to give me an example of a basic EM server making non-blocking ActiveRecord calls to a postgresql database (mainly because this is what I'll be using).
Also, there's one more thing I do not understand. EventMachine is mainly single threaded, but uses threads for defers. The default ruby interpreter only has green threads. How can EventMachine use system threads? Is it because it's mainly C++?
Thanks for your help.
Upvotes: 1
Views: 1432
Reputation: 18835
ActiveRecord should be able to work in a multithreaded environment. It depends on the database-driver to support parallel execution (mysql2 driver ie).
Upvotes: 1