Function and usage of PHP libevent

Time:2021-5-10

This paper introduces the functions and usage of PHP libevent. The details are as follows:

Libevent is a high performance network library based on event driven. Support a variety of I / O multiplexing technologies, such as epoll, poll, dev / poll, select and kqueue; Support I / O, timer, signal and other events; Register the event priority.

The constants are as follows:

value Constant name meaning
1 EV_TIMEOUT The event becomes active after the time has passed
2 EV_READ When FD is ready and can be read, the event becomes active
4 EV_WRITE When FD is ready and can be written, the event becomes active
8 EV_SIGNAL It is used to realize signal detection
16 EV_PERSIST Indicates that the event is persistent
32 EV_ET Indicates whether the underlying layer supports edge triggered events
1 EVLOOP_ONCE If evloop is set_ Once, the loop will wait for some events to become active, execute the active events until no more events can be executed, and then return.
2 EVLOOP_NONBLOCK If evloop is set_ Nonblock, the loop does not wait for an event to be triggered: the loop will only detect whether an event is ready and can be triggered immediately. If so, it will execute an event callback.

The function is as follows:

event_ base_ Free() releases the resource, which does not destroy the binding event
event_ base_ Loop () handles the event and handles the event loop according to the specified base
event_ base_ Loopbreak() immediately cancels the event loop with the same behavior as each break statement
event_ base_ Loopexit() exits the loop after the specified time
event_ base_ New() creates and initializes the event
event_ base_ priority_ Init() sets the priority of the event
event_ base_ Set () associates an event to an event base
event_ buffer_ base_ Set () associates cached events to events_ base
event_ buffer_ Disable() to disable a cached event
event_ buffer_ Enable() enables a specified cached event
event_ buffer_ fd_ Set () changes the description of a cached file system
event_ buffer_ Free() releases the cache event
event_ buffer_ New() creates a new cache event
event_ buffer_ priority_ Priority setting of set() cache events
event_ buffer_ Read() reads the data in the cache event
event_ buffer_ set_ Callback() sets or resets the callback function for the cached event
event_ buffer_ timeout_ Set () sets the timeout time for a cached event
event_ buffer_ watermark_ Set sets the watermark mark of read / write events
event_ buffer_ Write() writes data to the cache event
event_ Add() adds an execution event to the specified setting
event_ Del() removes events from the set events
event_ Free() clears the event handle
event_ New () creates a new event
event_ Set () is ready for the event_ Add events to add

Example 1 is as follows:

<?php
//Create and initialize a new event library
$base = event_base_new();
//Create and return a new event resource
$event = event_new();
//Prepare an event
event_set($event, 0, EV_TIMEOUT, function() {
  echo "run...";
});
//Associate events to event library
event_base_set($event, $base);
//Add event
event_add($event, 5000000);
//Handles the event loop according to the specified event library
event_base_loop($base);

After waiting for 5 seconds, the above code prints out run… And the program ends.

Let’s modify the above code:

<?php
//Create and initialize a new event library
$base = event_base_new();
//Create and return a new event resource
$event = event_new();
//Prepare an event
event_set($event, 0, EV_TIMEOUT | EV_PERSIST, function() {
  echo "run...\r\n";
});
//Associate events to event library
event_base_set($event, $base);
//Add event
event_add($event, 1000000);
//Handles the event loop according to the specified event library
event_base_loop($base);

The above code will print run… Every second, just because we are in event_ EV is used in set_ Persist, indicating that the event is persistent.

Example 2 is as follows:

<?php
function printContent($fd, $events, $args) {
  $data = trim(fgets($fd));
  echo "input: ", $data, "\r\n";
}
 
//Create and initialize a new event library
$base = event_base_new();
//Create and return a new event resource
$event = event_new();
//Here, the standard input stream is passed into the function printcontent
$fd = STDIN;
event_set($event, $fd, EV_PERSIST | EV_TIMEOUT, 'printContent', array($event, $base));
//Associate events to event library
event_base_set($event, $base);
//Add event
event_add($event, 1000000);
//Handles the event loop according to the specified event library
event_base_loop($base);

Every one second, the code will read the user’s input from the standard input and print it out.

Example 3 is as follows:

<?php
//Create socket
$socket = stream_socket_server('tcp://0.0.0.0:8888', $errno, $errstr);
//Set resource flow to non blocking mode
stream_set_blocking($socket, 0);
 
//Create and initialize a new event library
$base = event_base_new();
//Create and return a new event resource
$event = event_new();
//Prepare an event
event_set($event, $socket, EV_PERSIST | EV_READ, 'accept', $base);
//Associate events to event library
event_base_set($event, $base);
//Add event
event_add($event);
//Handles the event loop according to the specified event library
event_base_loop($base);
 
function accept($socket, $events, $base) {
  //Get the socket after the client is connected
  $client = stream_socket_accept($socket);
  //Set the socket to non blocking mode
  stream_set_blocking($client, 0);
  //Create a new cache event
  $buffer = event_buffer_new($client, 'read', NULL, 'error', $client);
  //Associate cache events to event library
  event_buffer_base_set($buffer, $base);
  //Cache event timeout
  event_buffer_timeout_set($buffer, 30, 30);
  //Set the watermark mark for cache event reading and writing
  event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
  //Sets the priority of cached events
  event_buffer_priority_set($buffer, 10);
  //Enable cache events
  event_buffer_enable($buffer, EV_READ | EV_PERSIST);
  //It seems that it is necessary to assign $buffer to a global variable
  $GLOBALS['_'] = $buffer;
}
 
function read($buffer, $client) {
  //Read data from cache events
  while ($read = event_buffer_read($buffer, 256)) {
  }
  //Write data to the connected socket client
  fwrite($client, date('Y-m-d H:i:s'));
  error($buffer, '', $client);
}
 
function error($buffer, $error, $client) {
  //Disable a cache event
  event_buffer_disable($buffer, EV_READ | EV_WRITE);
  //Release cache events
  event_buffer_free($buffer);
  //Close client socket
  fclose($client);
}

Attachment: installing libevent in PHP under Linux

1、 Install the libevent Library

1. Download and install the source code from the official website of libevent

http://libevent.org/

For example: libevent-2.0.22-stable.tar.gz

2. Unzip source code package


> tar zxvf libevent-2.0.22-stable.tar.gz
> cd libevent-2.0.22-stable

3、configure


> ./configure --prefix=/usr/local/libevent

4、make && make install


> make
> make install

2、 Install the libevent extension of PHP

1. The download address is as follows:

http://pecl.php.net/package/libevent

For example: libevent-0.1.0.tgz

2. Decompression


> tar zxvf libevent-0.1.0.tgz

3. Generate configure by phpize command


> /data/nmp/php/bin/phpize

(* the above path is for demonstration only, depending on the installation environment.)

4. After generating, run configure


> ./configure --with-php-config=/data/nmp/php/bin/php-config --with-libevent=/usr/local/libevent

5、make && make install


> make
> make install

6. Modify the php.ini file and add the following


extension=libevent.so

7. Restart the server

For more information about PHP, readers interested in it can see the following topics: PHP extension development tutorial, PHP network programming skills summary, PHP curl usage summary, PHP array operation skills encyclopedia, PHP data structure and algorithm tutorial, PHP programming algorithm summary and PHP string Usage Summary

I hope this article is helpful for PHP programming.