Coroutine
class Coroutine
Constants
| DETACHED_CONTEXT_KEY |
|
Properties
| static protected bool | $reportExceptions | ||
| static protected callable[] | $afterCreatedCallbacks |
Methods
Returns the current coroutine ID.
Register a callback to be called after a coroutine is created.
Register a callback to be executed when the coroutine exits.
Sleep for the given number of seconds.
Returns the parent coroutine ID.
Create a new coroutine.
Create a coroutine whose lifecycle is owned by a wrapper.
Create a coroutine with a copy of the parent coroutine context.
Wait for the given coroutines to finish.
Determine if currently running in a coroutine.
Get coroutine statistics.
Determine if a coroutine with the given ID exists.
Get a list of all coroutine IDs.
Determine if the given exception was caused by cancellation, including errors raised during cleanup.
Keep the first failure unless a later failure carries this coroutine's cancellation.
Enable or disable exception reporting in coroutines.
Report an exception without allowing cancellation to escape a terminal coroutine boundary.
Flush all static state.
Details
at line 36
static int
id()
Returns the current coroutine ID.
Returns -1 when running in non-coroutine context.
at line 50
static void
afterCreated(callable $callback)
Register a callback to be called after a coroutine is created.
The callback persists in a static property for the worker lifetime and runs for every subsequently created coroutine. Callbacks run synchronously during child startup and must not suspend. Hooks that inherit parent context must honor DETACHED_CONTEXT_KEY while preserving values explicitly installed in the child.
at line 59
static void
defer(callable $callable)
Register a callback to be executed when the coroutine exits.
at line 73
static void
sleep(float $seconds)
Sleep for the given number of seconds.
at line 87
static int
parentId(int|null $coroutineId = null)
Returns the parent coroutine ID.
Returns 0 when running in the top level coroutine.
at line 95
static int
create(callable $callable)
Create a new coroutine.
at line 110
static int
createOwned(callable $callable, Closure $wrapper, bool $detached = false)
Create a coroutine whose lifecycle is owned by a wrapper.
The wrapper runs at native child entry and must invoke the supplied runner exactly once. Outside the runner it must not wait for I/O or other work. Finalize ownership before notifying waiters, which may run immediately. Detachment prevents implicit parent-context propagation during startup.
at line 120
static int
fork(callable $callable, array $keys = [])
Create a coroutine with a copy of the parent coroutine context.
at line 141
static int
forkOwned(callable $callable, Closure $wrapper, array $keys = [], bool $detached = false)
Create an owned coroutine with a copy of the parent coroutine context.
The wrapper runs at native child entry and must invoke the supplied runner exactly once. Outside the runner it must not wait for I/O or other work. Finalize ownership before notifying waiters, which may run immediately. Detachment prevents parent fallback without discarding the copied context.
at line 207
static bool
join(array $coroutineIds, float $timeout = -1)
Wait for the given coroutines to finish.
A false return may mean that no supplied coroutine remained active or that the timeout elapsed. It is not a general failure signal.
at line 215
static bool
inCoroutine()
Determine if currently running in a coroutine.
at line 223
static array
stats()
Get coroutine statistics.
at line 233
static bool
exists(int $id)
Determine if a coroutine with the given ID exists.
at line 243
static iterable
list()
Get a list of all coroutine IDs.
at line 251
static bool
causedByCancellation(Throwable $exception)
Determine if the given exception was caused by cancellation, including errors raised during cleanup.
at line 279
static Throwable
preferCancellation(Throwable|null $kept, Throwable $next)
Keep the first failure unless a later failure carries this coroutine's cancellation.
at line 292
static void
reportExceptions(bool $report = true)
Enable or disable exception reporting in coroutines.
The flag persists in a static property for the worker lifetime and applies to every coroutine spawned across the process.
at line 303
static protected void
printLog(Throwable $throwable)
Report an exception through the exception handler.
at line 339
static protected void
reportUncaught(Throwable $throwable)
Report an exception without allowing cancellation to escape a terminal coroutine boundary.
at line 358
static void
flushState()
Flush all static state.