Home  >  Article  >  Backend Development  >  How to install Libevent extension for PHP

How to install Libevent extension for PHP

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-30 15:46:041921browse

libevent is an event-triggered network library, suitable for windows, linux, freebsd and other platforms. It internally uses system calls such as select, poll, epoll, kqueue and other system calls to manage event mechanisms. Let’s learn with the editor how to install the Libevent extension.

How to install Libevent extension for PHP

1. Introduction to libevent

libevent is an event-triggered network library, suitable for windows, linux, freebsd and other platforms. It uses select, System calls such as poll, epoll, and kqueue manage event mechanisms. Libevent is cross-platform and has extraordinary performance. It is event-driven like nodejs; official website: http://libevent.org/

Latest stable version

https://github.com/downloads/libevent/libevent/libevent -2.0.16-stable.tar.gz

Main modules:

Event processing framework

Event engine module

Buffer management module

Signal processing module

php itself does not support multi-threading, and php cannot implement the concurrency mechanism very well. The pcntl (process control), libevent extension, socket package, and stream system functions provided by Pecl can easily develop high-performance, high-concurrency network applications using PHP.

Simple application example: Pcntl fork n workers. After the master process gets the request, the processing information is sent to the worker program, and the worker sends it back to the client after processing. Master process can set the number of workers, that is, the size of n, according to the amount of concurrency, and monitor the data of workers, and start more processes when there is insufficient. Same as nginx principle.

Attachment: Php network programming framework

http://code.google.com/p/swoole/downloads/list

2. Why should you learn libevent

http server can be said to be a classic application of libevent. You can find the standard writing method of http from libevent. The non-blocking http server is the intertwining of socket processing and http protocol processing.

Learning libevent can help improve your programming skills. In addition to network programming, Libevent's code has many useful design techniques and basic data structures, such as information hiding, function pointers, and polymorphism in C language. Support, linked lists, heaps, etc., all help to improve one's programming skills [1].

Three types of request processing:

  • connection input fork a new process

  • connection input pthread_create

  • connection input throw a Event-based array; main process do nonblocking things;

3. Php libevent extension module installation

Curl –O https://github.com/downloads/libevent/libevent/libevent-2.0.16-stable.tar.gz

Tar –zxvf libevent-2.0.16-stable.tar.gz
Cd libevent-2.0.16-stable
/usr/local/php/bin/phpize
./configure
make
make install
vi /usr/local/php/etc/php.ini
extension_dir=””;
extension=libevent.so
php –m | grep lib

Installation successful

Recommended learning:php Video tutorial

The above is the detailed content of How to install Libevent extension for PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:imooc.com. If there is any infringement, please contact admin@php.cn delete