Skip to content

jsor-labs/event-loop

 
 

Repository files navigation

EventLoop Component

Build Status Code Climate

Event loop abstraction layer that libraries can use for evented I/O.

In order for async based libraries to be interoperable, they need to use the same event loop. This component provides a common LoopInterface that any library can target. This allows them to be used in the same loop, with one single run call that is controlled by the user.

The master branch contains the code for the upcoming 0.5 release. For the code of the current stable 0.4.x release, checkout the 0.4 branch.

In addition to the interface there are some implementations provided:

  • StreamSelectLoop: This is the only implementation which works out of the box with PHP. It does a simple select system call. It's not the most performant of loops, but still does the job quite well.

  • LibEventLoop: This uses the libevent pecl extension. libevent itself supports a number of system-specific backends (epoll, kqueue).

  • LibEvLoop: This uses the libev pecl extension (github). It supports the same backends as libevent.

  • ExtEventLoop: This uses the event pecl extension. It supports the same backends as libevent.

All of the loops support these features:

  • File descriptor polling
  • One-off timers
  • Periodic timers
  • Deferred execution of callbacks

Usage

Here is an async HTTP server built with just the event loop.

    $server = stream_socket_server('tcp://127.0.0.1:8080');
    stream_set_blocking($server, 0);
    React\EventLoop\addReadStream($server, function ($server) {
        $conn = stream_socket_accept($server);
        $data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
        React\EventLoop\addWriteStream($conn, function ($conn) use (&$data) {
            $written = fwrite($conn, $data);
            if ($written === strlen($data)) {
                fclose($conn);
                React\EventLoop\removeStream($conn);
            } else {
                $data = substr($data, $written);
            }
        });
    });

    React\EventLoop\addPeriodicTimer(5, function () {
        $memory = memory_get_usage() / 1024;
        $formatted = number_format($memory, 3).'K';
        echo "Current memory usage: {$formatted}\n";
    });

About

React's core reactor event-loop

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 98.4%
  • Shell 1.6%
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy