Recommend this page to a friend! |
Classes of Stanley Aloh | Tic Tac Toe Multiplayer | vendor/react/event-loop/README.md | Download |
|
DownloadEventLoop ComponentReactPHP's core reactor event loop 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 Table of Contents
Quickstart exampleHere is an async HTTP server built with just the event loop.
See also the examples. UsageTypical applications use a single event loop which is created at the beginning and run at the end of the program.
FactoryThe create()The
This method always returns an instance implementing This method should usually only be called once at the beginning of the program. Loop implementationsIn addition to the All of the event loops support these features:
For most consumers of this package, the underlying event loop implementation is
an implementation detail.
You should use the Advanced! If you explicitly need a certain event loop implementation, you can
manually instantiate one of the following classes.
Note that you may have to install the required PHP extensions for the respective
event loop implementation first or they will throw a StreamSelectLoopA This uses the This event loop works out of the box on PHP 5.3 through PHP 7+ and HHVM.
This means that no installation is required and this library works on all
platforms and supported PHP versions.
Accordingly, the Under the hood, it does a simple If you want to use signal handling (see also This event loop is known to rely on wall-clock time to schedule future timers
when using any version before PHP 7.3, because a monotonic time source is
only available as of PHP 7.3 ( ExtEventLoopAn This uses the This loop is known to work with PHP 5.4 through PHP 7+. ExtEvLoopAn This loop uses the This loop is known to work with PHP 5.4 through PHP 7+. ExtUvLoopAn This loop uses the This loop is known to work with PHP 7+. ExtLibeventLoopAn This uses the This event loop does only work with PHP 5.
An unofficial update for
PHP 7 does exist, but it is known to cause regular crashes due to This event loop is known to trigger a readable listener only if
the stream becomes readable (edge-triggered) and may not trigger if the
stream has already been readable from the beginning.
This also implies that a stream may not be recognized as readable when data
is still left in PHP's internal stream buffers.
As such, it's recommended to use ExtLibevLoopAn This uses an unofficial This loop does only work with PHP 5. An update for PHP 7 is unlikely to happen any time soon. LoopInterfacerun()The For many applications, this method is the only directly visible invocation on the event loop. As a rule of thumb, it is usally recommended to attach everything to the same loop instance and then run the loop once at the bottom end of the application.
This method will keep the loop running until there are no more tasks to perform. In other words: This method will block until the last timer, stream and/or signal has been removed. Likewise, it is imperative to ensure the application actually invokes this method once. Adding listeners to the loop and missing to actually run it will result in the application exiting without actually waiting for any of the attached listeners. This method MUST NOT be called while the loop is already running.
This method MAY be called more than once after it has explicity been
stop()The This method is considered advanced usage and should be used with care. As a rule of thumb, it is usually recommended to let the loop stop only automatically when it no longer has anything to do. This method can be used to explicitly instruct the event loop to stop:
Calling this method on a loop instance that is not currently running or on a loop instance that has already been stopped has no effect. addTimer()The The timer callback function MUST be able to accept a single parameter, the timer instance as also returned by this method or you MAY use a function which has no parameters at all. The timer callback function MUST NOT throw an Unlike
See also example #1. If you want to access any variables within your callback function, you can bind arbitrary data to a callback closure like this:
This interface does not enforce any particular timer resolution, so special care may have to be taken if you rely on very high precision with millisecond accuracy or below. Event loop implementations SHOULD work on a best effort basis and SHOULD provide at least millisecond accuracy unless otherwise noted. Many existing event loop implementations are known to provide microsecond accuracy, but it's generally not recommended to rely on this high precision. Similarly, the execution order of timers scheduled to execute at the same time (within its possible accuracy) is not guaranteed. This interface suggests that event loop implementations SHOULD use a monotonic time source if available. Given that a monotonic time source is only available as of PHP 7.3 by default, event loop implementations MAY fall back to using wall-clock time. While this does not affect many common use cases, this is an important distinction for programs that rely on a high time precision or on systems that are subject to discontinuous time adjustments (time jumps). This means that if you schedule a timer to trigger in 30s and then adjust your system time forward by 20s, the timer SHOULD still trigger in 30s. See also event loop implementations for more details. addPeriodicTimer()The The timer callback function MUST be able to accept a single parameter, the timer instance as also returned by this method or you MAY use a function which has no parameters at all. The timer callback function MUST NOT throw an Unlike
See also example #2. If you want to limit the number of executions, you can bind arbitrary data to a callback closure like this:
This interface does not enforce any particular timer resolution, so special care may have to be taken if you rely on very high precision with millisecond accuracy or below. Event loop implementations SHOULD work on a best effort basis and SHOULD provide at least millisecond accuracy unless otherwise noted. Many existing event loop implementations are known to provide microsecond accuracy, but it's generally not recommended to rely on this high precision. Similarly, the execution order of timers scheduled to execute at the same time (within its possible accuracy) is not guaranteed. This interface suggests that event loop implementations SHOULD use a monotonic time source if available. Given that a monotonic time source is only available as of PHP 7.3 by default, event loop implementations MAY fall back to using wall-clock time. While this does not affect many common use cases, this is an important distinction for programs that rely on a high time precision or on systems that are subject to discontinuous time adjustments (time jumps). This means that if you schedule a timer to trigger in 30s and then adjust your system time forward by 20s, the timer SHOULD still trigger in 30s. See also event loop implementations for more details. Additionally, periodic timers may be subject to timer drift due to re-scheduling after each invocation. As such, it's generally not recommended to rely on this for high precision intervals with millisecond accuracy or below. cancelTimer()The See also Calling this method on a timer instance that has not been added to this loop instance or on a timer that has already been cancelled has no effect. futureTick()The This works very much similar to timers with an interval of zero seconds, but does not require the overhead of scheduling a timer queue. The tick callback function MUST be able to accept zero parameters. The tick callback function MUST NOT throw an If you want to access any variables within your callback function, you can bind arbitrary data to a callback closure like this:
Unlike timers, tick 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).
See also example #3. addSignal()The This is useful to catch user interrupt signals or shutdown signals from
tools like The listener callback function MUST be able to accept a single parameter, the signal added by this method or you MAY use a function which has no parameters at all. The listener callback function MUST NOT throw an
See also example #4. Signaling is only available on Unix-like platform, Windows isn't
supported due to operating system limitations.
This method may throw a Note: A listener can only be added once to the same signal, any attempts to add it more then once will be ignored. removeSignal()The
Any attempts to remove listeners that aren't registered will be ignored. addReadStream()> Advanced! Note that this low-level API is considered advanced usage. Most use cases should probably use the higher-level readable Stream API instead. The The first parameter MUST be a valid stream resource that supports
checking whether it is ready to read by this loop implementation.
A single stream resource MUST NOT be added more than once.
Instead, either call The listener callback function MUST be able to accept a single parameter, the stream resource added by this method or you MAY use a function which has no parameters at all. The listener callback function MUST NOT throw an If you want to access any variables within your callback function, you can bind arbitrary data to a callback closure like this:
See also example #11. You can invoke The execution order of listeners when multiple streams become ready at the same time is not guaranteed. Some event loop implementations are known to only trigger the listener if
the stream becomes readable (edge-triggered) and may not trigger if the
stream has already been readable from the beginning.
This also implies that a stream may not be recognized as readable when data
is still left in PHP's internal stream buffers.
As such, it's recommended to use addWriteStream()> Advanced! Note that this low-level API is considered advanced usage. Most use cases should probably use the higher-level writable Stream API instead. The The first parameter MUST be a valid stream resource that supports
checking whether it is ready to write by this loop implementation.
A single stream resource MUST NOT be added more than once.
Instead, either call The listener callback function MUST be able to accept a single parameter, the stream resource added by this method or you MAY use a function which has no parameters at all. The listener callback function MUST NOT throw an If you want to access any variables within your callback function, you can bind arbitrary data to a callback closure like this:
See also example #12. You can invoke The execution order of listeners when multiple streams become ready at the same time is not guaranteed. removeReadStream()The Removing a stream from the loop that has already been removed or trying to remove a stream that was never added or is invalid has no effect. removeWriteStream()The Removing a stream from the loop that has already been removed or trying to remove a stream that was never added or is invalid has no effect. InstallThe recommended way to install this library is through Composer. New to Composer? This project follows SemVer. This will install the latest supported version:
See also the CHANGELOG for details about version upgrades. This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 7+ and HHVM. It's highly recommended to use PHP 7+ for this project. Installing any of the event loop extensions is suggested, but entirely optional. See also event loop implementations for more details. TestsTo run the test suite, you first need to clone this repo and then install all dependencies through Composer:
To run the test suite, go to the project root and run:
LicenseMIT, see LICENSE file. More
|