Libevent Learning in PHP

Source: Internet
Author: User
Tags epoll

[Email protected],1,3

Directory

Application Learning of libevent in PHP

1, libevent introduction

2, Why to learn libevent

3, Php libevent expansion module installation

4. Libevent constants and PHP functions

5. select/poll model

6. epoll/kqueue model

1, libevent introduction

Libevent is an event-triggered network library for many platforms such as Windows, Linux, andFreeBSD, with internal use of Select,poll,Epoll, Kqueue such as system invoke management event mechanism. The libevent is cross-platform and has extraordinary performance. as event-driven as Nodejs; Official website:http://libevent.org/

The 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 does not natively support multi-threading,PHP The concurrency mechanism cannot be implemented well. Pecl provided by Pcntl (Process Control),libevent extensions,sockets package,stream system functions, you can use PHP easy to develop high-performance, high-concurrency network applications.

Simple Application case: pcntl fork n worker . master process   after the request, processing information sent to worker Program , worker back to client when finished processing . master process &NBSP can be followed by the amount of concurrency, to set worker the number is n size, and monitor (monitoring) worker The data for the , which starts more processes when it is insufficient. Same as nginx principle.

attached:Php Network Programming Framework

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

2, why to learn libevent

HTTP server can be said to be a classic app for Libevent. The standard notation of HTTP can be found from libevent, which is the non-blocking HTTP server, which is the socket processing and the HTTP protocol processing.

Learning Libevent can help to improve the programming skills, in addition to network program design,libevent code has a lot of useful design techniques and basic data structure, such as information hiding, function pointers,C language Polymorphism support, linked list and heap, etc. [1] all contribute to improving their own procedural prowess.

There are three types of processing for requests:

1. connection Input Fork a new process

2. Connection Input Pthread_create

3, connection input throw a event-based array; main process do nonblocking things;

3, Php libevent expansion 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

4. libevent Constants and PHP functions

#define Ev_timeout 0x01

#define Ev_read 0x02

#define Ev_write 0x04

#define Ev_signal 0x08

#define Ev_persist 0x10

#define EV_ET 0x20

#define Evloop_once 0x01

#define Evloop_nonblock 0x02

Value

Constant name

Meaning

1

Ev_timeout

Event becomes active after time has passed

2

Ev_read

FD ready, can read when the event becomes active state

4

Ev_write

When FD is ready, the event becomes active when it can be written

8

Ev_signal

For signal detection

16

Ev_persist

Indicates that the event is persistent

32

Ev_et

Indicates whether the underlying edge trigger event is supported

1

Evloop_once

If Evloop_once is set , the loop waits for certain events to become active, and executes the active event until no more events can be executed and will be returned.

2

Evloop_nonblock

If Evloop_nonblock is set , the loop does not wait for the event to be triggered: the loop will only detect if an event is ready to be triggered immediately, and if so, the callback that executes the event.

Event_base_freee ()

Frees the resource, which cannot destroy the binding event

Event_base_loop ()

Handling events, handling 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 a loop after a specified time

Event_base_new ()

Create and initial events

Event_base_priority_init ()

Prioritize events

Event_base_set ()

Associating events to event base

Event_buffer_base_set ()

Associating cached events to event_base

Event_buffer_disable ()

To disable a cached event

Event_buffer_enable ()

To 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

To set a watermark token for read-write events

Event_buffer_write ()

Write data to the cache event

Event_add ()

Adds an execution event to the specified settings

Event_del ()

To remove an event from a set event

Event_free ()

Emptying the event handle

Event_new ()

To create a new event

Event_set ()

Prepare to add an event in Event_add

5. select/Poll model

If you use sockets extension, here are socket_select (),Stream_select (). For a large number of applications, thesocket_select (),stream _select () is already a good enough system call interface.

6. epoll/kqueue model

<?php

function Print_line ($FD, $events, $arg)

{

static $max _requests = 0;

$max _requests++;

if ($max _requests = = 10) {

Exit Loop after Ten 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);

Reference:

[1] http://blog.csdn.net/sparkliang/article/details/4957667

[2] http://blog.csdn.net/laoyi19861011/article/details/6539244

[3] http://blog.csdn.net/laoyi19861011/article/details/6537859

[4] http://www.wangafu.net/~nickm/libevent-book/Ref3_eventloop.html

[5] http://www.ooso.net/archives/607

[6] http://blog.csdn.net/vaal_water/article/details/6968695

[7] http://blog.csdn.net/shagoo/article/details/6396089

[8] http://blog.si.kz/index.php/2010/02/03/libevent-for-php

[9] http://www.ibm.com/developerworks/cn/aix/library/au-libev/index.html

[Ten] Https://github.com/kakserpom/phpdaemon

[One] http://www.cnblogs.com/imvkmark/archive/2011/12/21.html

[ http://www.oschina.net/question/12_15837 ]

Libevent Learning in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.