Code Monkey home page Code Monkey logo

event-loop's Introduction

EventLoop Component

Build Status

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.

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.

    $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, 0, $written);
            }
        });
    });

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

    $loop->run();

Note: The factory is just for convenience. It tries to pick the best available implementation. Libraries SHOULD allow the user to inject an instance of the loop. They MAY use the factory when the user did not supply a loop.

event-loop's People

Contributors

cameronjacobson avatar cboden avatar clue avatar daverandom avatar igorw avatar jmalloc avatar lt avatar nrk avatar pborreli avatar steverhoades avatar wyrihaximus avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.