I use libev to develope my event-driven app. I like to query remote mysql server inside events. so, Do mysql_real_connect block whole application or just my_read_cb
according to following code
my_read_cb(EV_P_ ev_io *w, int revents) {
mysql_real_connect(*mysql, "host", "user", "pass", "db", 3306, NULL, 0);
}
struct ev_loop *loop = ev_default_loop(0);
ev_io_init(io, my_read_cb, network_fd, EV_READ);
ev_io_start(loop, io);
ev_run(loop, 0);
It blocks the whole application because the callback function my_read_cb()
is executed in the same (aka main) thread as ev_run()
function.
This is how reactor pattern works, your code should be "non-blocking" which means that you should avoid any I/O waits, sleep()
calls, mutex waits etc. It is difficult to follow such a requirement with traditional blocking code from various libraries such as MySQL driver in your case.
There are (at least) three ways how to solve it:
getaddrinfo
family).