serialx serialx - 13 days ago 10
MySQL Question

Using Mysql blocking API with Boost::asio

I am building a aync single threaded server that receives data from clients. It processes the data and then saves it to the MySQL Database.

The problem is that, MySQL C API does not support non-blocking calls and asio mainly does not like blocking calls.

So I am thinking something like Python Twisted's deferToThread() like semantics. Is there anyone who already developed such thing? Or should I implement it?

Answer

There was a post to the Asio mailing list over the summer describing a generic asynchronous service class that seems like it might be useful for you. This pseudo code is from the author's email I linked:

// Create a command processor with 5 threads allocated
// to processing the commands.
async_command_processor processor(io_service, 5);

// Execute the command asynchronously and call the
// MyCommandComplete callback when completed.
processor.async_execute(MyCommand, MyCommandComplete);