Libevent is a high-performance network library based on event-driven. Supports multiple I/O multiplexing Technologies, Epoll, poll, Dev/poll, select, and Kqueue, support I/O, Timer and signal events, and register event priorities.
PHP Libevent Extended Installation:
The libevent extension relies on the original Libevent library, and the Libevent library must be installed first.
(1) Install Libevent Library
wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
Tar zxvf libevent-2.0.20-stable.tar.gz
CD libevent-2.0.20-stable/
./configure--prefix=/usr/local/libevent-2.0.20/
Make
Make install
(2) Install libevent extension (http://pecl.php.net/package/libevent)
wget http://pecl.php.net/get/libevent-0.1.0.tgz
TAR-ZXVF libevent-0.1.0.tgz
CD libevent-0.1.0
./configure--with-php-config=/usr/local/php54/bin/php-config--with-libevent=/usr/local/libevent-2.0.20/
Make && make install
#php. ini Add extension=libevent.so
PHP libevent Extension Introduction:
(1) constant
Libevent1.png
(2) function
Event_base_free () frees resources, this does not destroy binding events
Event_base_loop () Handles events, handling event loops according to the specified base
Event_base_loopbreak () immediately cancels the event loop, and the behavior each break statement is the same
Event_base_loopexit () exits the loop after a specified time
Event_base_new () creates and the initial event
Event_base_priority_init () sets the priority of the event
Event_base_set () associates events to event base
Event_buffer_ Base_set () Associates cached events to Event_base
Event_buffer_disable () Disable a cached event
Event_buffer_enable () enable a specified cache event
Event_ Buffer_fd_set () change a cached file system description
Event_buffer_free () Free cache events
Event_buffer_new () to create a new cache event
Event_buffer_ Priority_set () Priority setting for cache events
Event_buffer_read () read the data in the cache event
Event_buffer_set_callback () set or reset the callback Hansh function to the cached event
Event_buffer_timeout_set () sets a timed read/write time for a cached event
Event_buffer_watermark_set set the watermark marker for read-write events
Event_buffer_write () writes data to a cached event
Event_add () adds an execution event to the specified setting
Event_del () removes the event from the Set event
Event_free () empties the event handle
Event_new () Create a new event
Event_set () ready to add an event to the Event_add
PHP libevent Extension use:
1:5s triggers callback
$base = Event_base_new ();
$event = Event_new ();
Event_ Set ($event, 0, ev_timeout, function () {
echo "function called";
});
Event_base_set ($event, $base);
Event_add ($event, 5000000);
Event_base_loop ($base);
Example 2: Print input stream
function Print_line ($FD, $events, $arg)
{
static $max _requests = 0;
$max _requests++;
if ($max _requests = = 10) {
Exit Loop after writes
Event_base_loopexit ($arg [1]);
}
Echo fgets ($FD);
}
Create Base and Event
$base = Event_base_new ();
$event = Event_new ();
$FD = STDIN;
Set Event Flags
Event_set ($event, $FD, Ev_read | Ev_persist, ' Print_line ', Array ($event, $base));
Set Event Base
Event_base_set ($event, $base);
Enable Event
Event_add ($event);
Start Event Loop
Event_base_loop ($base);
Example 3: Implementing a simple Web server
After the CLI executes, try opening browser Port 2000.
$socket = Stream_socket_server (' tcp://0.0.0.0:2000 ', $errno, $ERRSTR);
Stream_set_blocking ($socket, 0);
$base = Event_base_new ();
$event = Event_new ();
Event_set ($event, $socket, Ev_read | Ev_persist, ' ev_accept ', $base);
Event_base_set ($event, $base);
Event_add ($event);
Event_base_loop ($base);
function ev_accept ($socket, $flag, $base)
{
$connection = Stream_socket_accept ($socket);
Stream_set_blocking ($connection, 0);
$buffer = Event_buffer_new ($connection, ' Ev_read ', NULL, ' Ev_error ', $connection);
Event_buffer_base_set ($buffer, $base);
Event_buffer_timeout_set ($buffer, 30, 30);
Event_buffer_watermark_set ($buffer, Ev_read, 0, 0XFFFFFF);
Event_buffer_priority_set ($buffer, 10);
Event_buffer_enable ($buffer, Ev_read | Ev_persist);
$GLOBALS [' _ '] = $buffer; This buffer must be assigned to a global variable seemingly a bug in the process of passing a value or a 5.3.8 closure or is there a problem?
}
function Ev_error ($buffer, $error, $connection)
{
Event_buffer_disable ($buffer, Ev_read | Ev_write);
Event_buffer_free ($buffer);
Fclose ($connection);
}
function Ev_read ($buffer, $connection)
{
while ($read = Event_buffer_read ($buffer, 256)) {
}
Fwrite ($connection, date (' y-m-d h:i:s '));
Ev_error ($buffer, ', $connection);
}