Libevent is an event-driven, high-performance network library. Supports multiple I/O multiplexing Technologies, Epoll, poll, Dev/poll, select, and Kqueue, support for events such as I/O, timers and Signals, and register event priorities.
PHP libevent Extension Installation:
The libevent extension relies on the original Libevent library, and the Libevent library must be installed first.
(1) Installing the Libevent Library
wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gztar zxvf libevent-2.0. -stable.tar.gzcd libevent-2.0. -stable/. /configure--prefix=/usr/local/libevent-2.0. /makemake 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
#使用phpize生成configure文件
. /configure--with-php-config=/usr/local/php54/bin/php-config--with-libevent=/usr/local/libevent-2.0. && make install#php.ini add extension=libevent.so
Introduction to PHP libevent Extensions:
(1) Constants
(2) function
- Event_base_free () frees the resource, which cannot destroy the binding event
- Event_base_loop () Handles events and handles event loops based on the specified base
- Event_base_loopbreak () immediately cancels the event loop, with the same behavior for each break statement
- Event_base_loopexit () exits the loop after a specified time
- Event_base_new () creation and initial events
- Event_base_priority_init () Sets the priority of the event
- Event_base_set () Associating events to event base
- Event_buffer_base_set () Associating cached events to Event_base
- Event_buffer_disable () disabling a cached event
- Event_buffer_enable () enable a specified cached event
- Event_buffer_fd_set () Change the file system description of a cache
- Event_buffer_free () releasing cache events
- Event_buffer_new () to create a new cache event
- Event_buffer_priority_set () Priority setting for cache events
- Event_buffer_read () reading data from a cache event
- Event_buffer_set_callback () Sets or resets the callback Hansh function to the cached event
- Event_buffer_timeout_set () sets the read and write time to timeout for a cached event
- Event_buffer_watermark_set setting watermark flags for read and write events
- Event_buffer_write () writes data to the cache event
- Event_add () Adds an execution event to the specified settings
- Event_del () Removing events from the set events
- Event_free () empty event handle
- Event_new () Create a new event
- Event_set () Prepare to add an event in Event_add
PHP libevent Extensions use:
Example 1:5s after triggering callback
$Base = event_base_new (); $Event = event_new (); Event_set ($event0 , Ev_timeout, function () { "function called";}); Event_base_set ($event, $base); Event_add ($event5000000); Event_base_loop ($base);
Example 2: printing an input stream
function Print_line ($FD, $events, $arg) {Static$max _requests =0; $max _requests++; if($max _requests = =Ten) { //exit Loop after ten writesEvent_base_loopexit ($arg [1]); } Echo fgets ($FD);} //Create base and event$Base=event_base_new (); $Event=event_new (); $FD=STDIN;//Set Event FlagsEvent_set ($Event, $FD, Ev_read | Ev_persist,'Print_line', Array ($Event, $Base));//Set Event BaseEvent_base_set ($Event, $Base);//Enable eventEvent_add ($Event);//Start Event LoopEvent_base_loop ($Base);
Example 3: Implementing a simple Web server
After the CLI executes, open the browser 2000 port and try again.
$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, -, -); Event_buffer_watermark_set ($buffer, Ev_read,0,0xFFFFFF); Event_buffer_priority_set ($buffer,Ten); Event_buffer_enable ($buffer, Ev_read|ev_persist); $GLOBALS ['_'] = $buffer;//This buffer must be assigned to a global variable that looks like a bug in the pass-through process or a 5.3.8 closure or 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, the) {} fwrite ($connection, date ('y-m-d h:i:s')); Ev_error ($buffer,"', $connection);}
Resources:
- http://blog.csdn.net/laoyi_grace/article/details/6539244
- Http://www.ibm.com/developerworks/cn/aix/library/au-libev/index.html
- http://blog.csdn.net/shagoo/article/details/6396089
Original:
Http://www.phpddt.com/php/libevent.html
PHP Libevent Extended Installation