开发者

libevent2 and file io

I've been toying around with libevent2, and I've got reading files working, but it blocks. Is there any way to make file rea开发者_如何学编程ding not block just within libevent. Or, do I need to use another IO library for files and make it pump events that I need.

fd = open("/tmp/hello_world",O_RDONLY);
evbuffer_read(buf,fd,4096);

The O_NONBLOCK flag doesn't work either.


In POSIX disks are considered "fast devices" meaning that they always block (which is why O_NONBLOCK didn't work for you). Only network sockets can be non-blocking.

There is POSIX AIO, but e.g. on Linux that comes with a bunch of restrictions making it unsuitable for general-purpose usage (only for O_DIRECT, I/O must be sector-aligned).

If you want to integrate normal POSIX IO into an asynchronous event loop it seems people resort to thread pools, where the blocking syscalls are executed in the background by one of the worker threads. One example of such a library is libeio


No.

I've yet to see a *nix where you can do non-blocking i/o on regular files without resorting to the more special AIO library (Though for some, e.g. solaris, O_NONBLOCK has an effect if e.g. someone else holds a lock on the file)


Please take a look at libuv, which is used by node.js / io.js: https://github.com/libuv/libuv

It's a good alternative to libeio, because it does perform well on all major operating systems, from Windows to the BSDs, Mac OS X and of course Linux. It supports I/O completion ports, which makes it a better choice than libeio if you are targeting Windows.

The C code is also very readable and I highly recommend this tutorial: https://nikhilm.github.io/uvbook/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜