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.

Table of Contents

Quickstart example

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

$loop = React\EventLoop\Factory::create();

$server = stream_socket_server('tcp://127.0.0.1:8080');
stream_set_blocking($server, 0);

$loop->addReadStream($server, function ($server) use ($loop) {
    $conn = stream_socket_accept($server);
    $data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
    $loop->addWriteStream($conn, function ($conn) use (&$data, $loop) {
        $written = fwrite($conn, $data);
        if ($written === strlen($data)) {
            fclose($conn);
            $loop->removeStream($conn);
        } else {
            $data = substr($data, $written);
        }
    });
});

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

$loop->run();

See also the examples.

Usage

Typical applications use a single event loop which is created at the beginning and run at the end of the program.

// [1]
$loop = React\EventLoop\Factory::create();

// [2]
$loop->addPeriodicTimer(1, function () {
    echo "Tick\n";
});

$stream = new React\Stream\ReadableResourceStream(
    fopen('file.txt', 'r'),
    $loop
);

// [3]
$loop->run();
  1. The loop instance is created at the beginning of the program. A convenience factory React\EventLoop\Factory::create() is provided by this library which picks the best available loop implementation.
  2. The loop instance is used directly or passed to library and application code. In this example, a periodic timer is registered with the event loop which simply outputs Tick every second and a readable stream is created by using ReactPHP's stream component for demonstration purposes.
  3. The loop is run with a single $loop->run() call at the end of the program.

Loop implementations

In addition to the interface there are the following 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 on future loop tick

futureTick()

The futureTick(callable $listener): void method can be used to schedule a callback to be invoked on a future tick of the event loop.

This works very much similar to timers with an interval of zero seconds, but does not require the overhead of scheduling a timer queue.

Unlike timers, callbacks are guaranteed to be executed in the order they are enqueued. Also, once a callback is enqueued, there's no way to cancel this operation.

This is often used to break down bigger tasks into smaller steps (a form of cooperative multitasking).

$loop->futureTick(function () {
    echo 'b';
});
$loop->futureTick(function () {
    echo 'c';
});
echo 'a';

See also example #3.

Install

The recommended way to install this library is through Composer. New to Composer?

This will install the latest supported version:

$ composer require react/event-loop

Tests

To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

$ composer install

To run the test suite, go to the project root and run:

$ php vendor/bin/phpunit

License

MIT, see LICENSE file.

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