Coroutine
class Coroutine
Constants
| DETACHED_CONTEXT_KEY |
|
Properties
| static protected bool | $enableReportException | ||
| 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.
Alias of Coroutine::parentId().
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.
Enable or disable exception reporting in coroutines.
Flush all static state.
Report an exception without allowing cancellation to escape a terminal coroutine boundary.
Details
at line 33
static int
id()
Returns the current coroutine ID.
Returns -1 when running in non-coroutine context.
at line 47
static void
afterCreated(callable $callback)
Register a callback to be called after a coroutine is created.
Boot-only. 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 55
static void
defer(callable $callable)
Register a callback to be executed when the coroutine exits.
at line 69
static void
sleep(float $seconds)
Sleep for the given number of seconds.
at line 82
static int
parentId(int|null $coroutineId = null)
Returns the parent coroutine ID.
Returns 0 when running in the top level coroutine.
at line 93
static int
pid(int|null $coroutineId = null)
Alias of Coroutine::parentId().
at line 101
static int
create(callable $callable)
Create a new coroutine.
at line 116
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 126
static int
fork(callable $callable, array $keys = [])
Create a coroutine with a copy of the parent coroutine context.
at line 147
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 214
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 222
static bool
inCoroutine()
Determine if currently running in a coroutine.
at line 230
static array
stats()
Get coroutine statistics.
at line 238
static bool
exists(int $id)
Determine if a coroutine with the given ID exists.
at line 248
static iterable
list()
Get a list of all coroutine IDs.
at line 259
static void
enableReportException(bool $enableReportException)
Enable or disable exception reporting in coroutines.
Boot-only. The flag persists in a static property for the worker lifetime and applies to every coroutine spawned across the process.
at line 267
static void
flushState()
Flush all static state.
at line 278
static protected void
printLog(Throwable $throwable)
Report an exception through the exception handler.
at line 310
static protected void
reportUncaught(Throwable $throwable)
Report an exception without allowing cancellation to escape a terminal coroutine boundary.