📜 ⬆️ ⬇️

PHP extension for asynchronous POSIX I / O

Background



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

This is a PECL eio extension that provides an interface to the libeio library.
')


libeio



Just in case, I will give a small 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, which can be useful on game servers, as the author of the library, M. Lehman writes:
"For a few seconds, 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, in the source code there is practically nothing to build (in the usual way) code in Windows. I have a feeling that the author was thinking about supporting Windows, but later abandoned this idea.

Eio extension



In the extension, libeio calls are made. In order to initiate the execution of a series of libeio calls, you need to cyclically call eio_poll (). For this, a function of the same name has been exported to PHP. Additionally, an event loop is implemented as a function eio_event_loop (), the IPC of which is based on semaphores. If desired, the user can perform his own event loop using existing PHP tools (for example, using global variables [preferably only in CLI] or the same semaphores using the Semaphore extension).

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

Installation



libeio :

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


eio :
$ 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


After that we write to the php.ini config
extension=eio.so
restart the Web server if it is not CLI.

If Gentoo has problems like
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


then the following commands will help:

$ phpize
$ aclocal && libtoolize --force && autoreconf
$ ./configure --with-eio # further optional parameters(--enable-eio-debug recommended for first use) ...
$ make
#


You can also use the libeio packages that I 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
xUbuntu_11.10 i586, x86_64
Unfortunately, the author of libeio offers only CVS.

However, eio can be installed easier:
# pecl install eio # stable
# pecl install eio-beta # beta
# pecl install eio-alpha # alpha


Alternatively, you can take the freshest of SVN:
$ svn co https://svn.php.net/repository/pecl/eio/trunk eio && cd eio
# pecl install --force package.xml

The last command must be given as root.

Finally



Thanks for attention. I would welcome any suggestions for the addition / change of the API.

PS
This eio was not easy.

UPDATE


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

Actually, eio added support for a file descriptor that can be used to effectively bundle with libevent, though only in SVN (see below).

A bunch of priorities (not just a real example, but who needs to write a real one;):
<?php
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 :

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

Source: https://habr.com/ru/post/131082/


All Articles