PHP extension for POSIX asynchronous I / O

Background



I venture to get a lot of criticism in the comments. However, I’m very interested to know what people think about the extension I recently finished writing. Perhaps someone will test, and the extension will become "beta-stable."

We are talking about the PECL eio extension , which provides an interface to the libeio library .



libeio



Just in case, I will give a short description of libeio. This is the C library for which runs system calls (mainly POSIX) in separate threads. The purpose of the library is completely non-blocking software that can be useful on game servers, as the author of the library, M. Lehman writes:
"... in a game server, you would not want to freeze for a few seconds just because the server is running a backup and you happen to call readdir. "


Those. calls such as open, read, write, close, readahead, truncate, rename, sendfile, statvfs, etc. in libeio run in separate threads.

The libeio documentation does not directly reject Windows. It is written that if there is no corresponding call in the library, then libeio emulates this call. However, there is practically nothing in the source code to compile (in the usual way) code in Windows. I have a feeling that the author was thinking about supporting Windows, but subsequently abandoned this idea.

Eio extension



The extension makes calls to libeio. To initiate a series of calls to libeio, you need to call eio_poll () cyclically. For this, the function of the same name has been exported to PHP. Additionally, an event loop is implemented in the form of the eio_event_loop () function, whose IPC is based on semaphores. If desired, the user can execute his own event loop with existing PHP tools (for example, using global variables [better only in the CLI] or the same semaphores using the Semaphore extension ).

Documentation: http://www.php.net/eio .

Installation



libeio : eio : After that, we write to the php.ini config and restart the web server if it is not a CLI. If problems arise in Gentoo , the following commands will help: You can also use the libeio packages that I have compiled for the following distributions: Debian_6.0 i586, x86_64 RedHat_RHEL-6 i586, x86_64 openSUSE_11.4 i586, x86_64 xUbuntu_10.04 i586, x86_64 xUb .10 i586, x86_64 Unfortunately, the author of libeio only offers CVS. However, eio can be installed easier: Alternatively, you can take the latest from SVN:

$ touch ~/.cvspass
$ cvs -z3 -d :pserver:anonymous@cvs.schmorp.de/schmorpforge co libeio
$ cd libeio
$ ./autogen.sh
$ ./configure
$ make
$ su -
# make install



$ wget 'http://pecl.php.net/get/eio' -O eio.tgz
$ tar xzvf eio.tgz && cd eio-*
$ phpize
$ ./configure --with-eio # further optional parameters(--enable-eio-debug recommended for first use) ...
$ make && make test
$ su -
# make install



extension=eio.so



libtool: Version mismatch error. This is libtool 2.4, but the
libtool: definition of this LT_INIT comes from an older release.
libtool: You should recreate aclocal.m4 with macros from libtool 2.4
libtool: and run autoconf again.
make: *** [libevent.lo] Error 63




$ phpize
$ aclocal && libtoolize --force && autoreconf
$ ./configure --with-eio # further optional parameters(--enable-eio-debug recommended for first use) ...
$ make
# дальше как обычно











# pecl install eio # для stable
# pecl install eio-beta # для beta
# pecl install eio-alpha # для alpha



$ svn co https://svn.php.net/repository/pecl/eio/trunk eio && cd eio
# pecl install --force package.xml

The last command must be given with root privileges

Finally



Thanks for attention. I will be glad to any suggestions to add / change the API.

PS
This eio was not easy.

UPDATE


As a result of working with the libevent author :
added: event_priority_set
added: support for numerical descriptors in event_set
added: support for numerical descriptors in event_buffer_new

Actually, eio adds support for a file descriptor that can be used to efficiently bind to libevent, however, so far only in SVN (see . below).

A bunch of priorities (not exactly the real example, but who needs to write the real one;): SVN :
function my_eio_poll($fd, $events, $arg) {
/* Some libevent regulation might go here .. */
if (eio_nreqs()) {
eio_poll();
}
/* .. and here */
}

function my_nop_cb($d, $r) {
var_dump($r); var_dump($d);
}

$base = event_base_new();
$event = event_new();

$fd = eio_get_event_stream();
var_dump($fd);

eio_nop(EIO_PRI_DEFAULT, "my_nop_cb", "nop data");
/* some other eio_* calls here ... */

// set event flags
event_set($event, $fd, EV_READ /*| EV_PERSIST*/, "my_eio_poll", array($event, $base));

// Set event priorities
event_base_priority_init($base, 10);

// set event base
event_base_set($event, $base);

// This one is my patch to libevent. Tony should apply it soon
echo "setting priority...\n";
var_dump(event_priority_set($event, 2));

// enable event
event_add($event);

// start event loop
event_base_loop($base);

/* The same kind of stuff is available via buffered libevent API*/
?>




svn.php.net/repository/pecl/libevent/trunk
svn.php.net/repository/pecl/eio/trunk

Also popular now: