Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

React's core reactor event-loop

License

NotificationsYou must be signed in to change notification settings

steverhoades/event-loop

 
 

Repository files navigation

Build StatusCode 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 thesame event loop. This component provides a commonLoopInterface that anylibrary can target. This allows them to be used in the same loop, with onesinglerun 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 thebox with PHP. It does a simpleselect system call. It's not the mostperformant of loops, but still does the job quite well.

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

  • LibEvLoop: This uses thelibev pecl extension(github). It supports the samebackends as libevent.

  • ExtEventLoop: This uses theevent pecl extension. It supports the samebackends 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 bestavailable implementation. LibrariesSHOULD allow the user to inject aninstance of the loop. TheyMAY use the factory when the user did not supplya loop.

About

React's core reactor event-loop

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP98.6%
  • Shell1.4%

[8]ページ先頭

©2009-2025 Movatter.jp