EvisaPECLextensionprovidingintefacetolibevlibrary & amp; ndash; highperformancefull-featuredeventloopwritteninC.ABOUTLIBEVLibevisaneventloop: 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 timer usage Stop (); // Stop the watcher if further callcause more than 10 iterations Ev: iteration () >=10 and $ w-> stop ();}); // Create a stopped timer and start it manually. $ 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 single 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 secondsiteration = 5Run single iterationCallback of a timer created as stoppedRestart 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 Example 2 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
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.