I’m using Goliath (which is powered by eventmachine) and the postgres gem pg, currently I’m using the pg gem in a blocking way: conn.exec('SELECT * FROM products') (for example) and I’m wondering whether there is a better way to connect to a postgres database?
I’m using Goliath (which is powered by eventmachine) and the postgres gem pg ,
Share
The
pglibrary provides full support for PostgreSQL’s asynchronous API. I’ve added an example of how to use it to thesamples/directory:I’d recommend that you read the documentation on the PQconnectStart function and the Asynchronous Command Processing section of the PostgreSQL manual, and then compare that with the sample above.
I haven’t used EventMachine before, but if it lets you register a socket and callbacks for when it becomes readable/writable, I’d think it’d be fairly easy to integrate database calls into it.
I’ve been meaning to use the ideas in Ilya Grigorik’s article on using Fibers to clean up evented code to make the async API easier to use, but that’s a ways off. I do have a ticket open to track it if you’re interested/motivated to do it yourself.