Manual
The event loop
The loop beneath the coroutines, for library authors.
Introduction#
Every coroutine runs on one event loop per process. Application code rarely touches it: run(), go() and await() are enough. A library that integrates its own I/O or timers needs more: phasync::getLoop returns the loop, PollerInterface is what waits for streams and sockets, phasync::service starts a background coroutine that does not keep the program alive, and phasync::isRunning and phasync::getFiber tell the library whether it is inside a coroutine and which one.
Concepts#
- Internals – The event loop, the pollers and the scheduler.
Guides#
- Use phasync in a library – Write concurrent library code that runs anywhere PHP runs.
Examples#
- All examples of this topic – 4 with their output.
Reference#
phasync::service– Starts $fn as a service: a coroutine of no frame, in the root scope, which outlives the code that started it, and returns it.phasync::isRunning– Returns true while a phasync::run is in progress, and false otherwise.phasync::getLoop– Returns the event loop, for code that waits with its low-level API (getSlot(), park() and unpark() of the returned object), or drives it in place (tick()).phasync::getFiber– Returns the running coroutine.DeadlockException– Coroutines wait, and nothing can ever wake them: the outermost run() throws this.PollerInterface– Waits for streams on behalf of the event loop.