Libev extension of php

Source: Internet
Author: User
Tags evio

Ev is a PECL extension providing inteface to libev library-high performance full-featured event loop written in C. about libev Libev is an event loop: you register interest in certain events (such as a file descriptor being readable or a timeout occurring ), and it will manage these event sources and provide your program with events. so what is libev? An Excerpt from the Internet: libev is a network library for high-performance event loop/event models, and contains a large number of new features. It is a new network library after lievent and Event perl module. It pursues the goal of faster speed, fewer bugs, more features, and smaller size. Www.2cto.com is similar to libevent. According to the author's introduction, it can be used as a replacement for libevent and provide higher performance. Complex configurations are not required. It seems to have a lot to do with the libevent mentioned earlier, but the author of this extension is obviously active and submitted three versions within a week. Code example: How to Use timer <? Php // create a timer and trigger $ w1 = new EvTimer (2, 0, function () {echo "2 seconds elapsed \ n" ;}after 2 seconds ";}); // create a timer and trigger it after 2 seconds, repeat every second // until we manually stop $ w2 = new EvTimer (2, 1, function ($ w) {echo "is called every second, is launched after 2 seconds \ n"; echo "iteration =", Ev: iteration (), PHP_EOL; // Stop the watcher after 5 iterations Ev: iteration () = 5 and $ w-> stop (); // Stop the watcher if further CILS cause more th An 10 iterations Ev: iteration () >=10 and $ w-> stop () ;}); // create a stopped timer, $ w_stopped = EvTimer: createStopped (10, 5, function ($ w) {echo "Callback of a timer created as stopped \ n "; // Stop the watcher after 2 iterations Ev: iteration () >=2 and $ w-> stop () ;}); // Loop until Ev: stop () is called or all of watchers stopEv: run (); // Start and look if it works $ w_stopped-> start (); echo "Run s Ingle iteration \ n "; Ev: run (Ev: RUN_ONCE); echo" Restart the second watcher and try to handle the same events, but don't block \ n "; $ w2-> again (); Ev: run (Ev: RUN_NOWAIT); $ w = new EvTimer (10, 0, function (){}); echo "Running a blocking loop \ n"; Ev: run (); echo "END \ n" ;?> Output content 2 seconds elapsedis called every second, is launched after 2 secondsiteration = 1is called every second, is launched after 2 secondsiteration = 2is called every second, is launched after 2 secondsiteration = 3is called every second, is launched after 2 secondsiteration = 4is called every second, is launched after 2 seconds = 5Run single iterationCallback of a timer created as stoppedR Estart the second watcher and try to handle the same events, but don't blockRunning a blocking loopis called every second, is launched after 2 secondsiteration = 8is called every second, is launched after 2 secondsiteration = 9is called every second, is launched after 2 secondsiteration = 10 ENDI/O Event Example 1 <? Php // Wait until STDIN is readable $ w = new EvIo (STDIN, Ev: READ, function ($ watcher, $ revents) {echo "STDIN is readable \ n" ;}); Ev: run (Ev: RUN_ONCE) ;?> Example 2 <? Php/* Use some async I/O to access a socket * // 'buckets' extension still logs warnings // for EINPROGRESS, EAGAIN/EWOULDBLOCK etc. error_reporting (E_ERROR); $ e_nonblocking = array (/* EAGAIN or EWOULDBLOCK */11,/* EINPROGRESS */115 ); // Get the port for the WWW service $ service_port = getservbyname ('www ', 'tcp '); // Get the IP address for the target host $ address = gethostbyname ('Google. co. uk '); // CRES Ate a TCP/IP socket $ socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); if ($ socket = FALSE) {echo "socket_create () failed: reason :". socket_strerror (socket_last_error ()). "\ n" ;}// Set O_NONBLOCK flagsocket_set_nonblock ($ socket); // Abort on timeout $ timeout_watcher = new EvTimer (10.0, 0 ., function () use ($ socket) {socket_close ($ socket); Ev: stop (Ev: BREAK_ALL) ;}); // Make HEAD request wh En the socket is writable $ write_watcher = new EvIo ($ socket, Ev: WRITE, function ($ w) use ($ socket, $ timeout_watcher, $ e_nonblocking) {// Stop timeout watcher $ timeout_watcher-> stop (); // Stop write watcher $ w-> stop (); $ in = "HEAD/HTTP/1.1 \ r \ n"; $ in. = "Host: google. co. uk \ r \ n "; $ in. = "Connection: Close \ r \ n"; if (! Socket_write ($ socket, $ in, strlen ($ in) {trigger_error ("Failed writing $ in to socket", E_USER_ERROR);} $ read_watcher = new EvIo ($ socket, ev: READ, function ($ w, $ re) use ($ socket, $ e_nonblocking) {// Socket is readable. recv () 20 bytes using non-blocking mode $ ret = socket_recv ($ socket, $ out, 20, MSG_DONTWAIT); if ($ ret) {echo $ out ;} elseif ($ ret = 0) {// All read $ w-> stop (); socket_close ($ Socket); return;} // Caught EINPROGRESS, EAGAIN, or EWOULDBLOCK if (in_array (socket_last_error (), $ e_nonblocking) {return ;}$ w-> stop (); socket_close ($ socket) ;}); Ev: run () ;}); $ result = socket_connect ($ socket, $ address, $ service_port); Ev :: run ();?> Output HTTP/1.1 301 Moved PermanentlyLocation: http://www.google.co.uk/Content-Type: text/html; charset = UTF-8Date: Sun, 23 Dec 2012 16:08:27 GMTExpires: Tue, 22 Jan 2013 16:08:27 GMTCache-Control: public, max-age = 2592000 Server: gwsContent-Length: 221X-XSS-Protection: 1; mode = blockX-Frame-Options: SAMEORIGINConnection: close

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.