class Coroutine

Properties

static protected bool $enableReportException
static protected callable[] $afterCreatedCallbacks

Methods

static int
id()

Returns the current coroutine ID.

static void
afterCreated(callable $callback)

Register a callback to be called after a coroutine is created.

static void
defer(callable $callable)

Register a callback to be executed when the coroutine exits.

static void
sleep(float $seconds)

Sleep for the given number of seconds.

static int
parentId(int|null $coroutineId = null)

Returns the parent coroutine ID.

static int
pid(int|null $coroutineId = null)

Alias of Coroutine::parentId().

static int
create(callable $callable)

Create a new coroutine.

static int
fork(callable $callable, array $keys = [])

Create a coroutine with a copy of the parent coroutine context.

static bool
join(array $coroutineIds, float $timeout = -1)

Wait for the given coroutines to finish.

static bool
inCoroutine()

Determine if currently running in a coroutine.

static array
stats()

Get coroutine statistics.

static bool
exists(int $id)

Determine if a coroutine with the given ID exists.

static iterable
list()

Get a list of all coroutine IDs.

static void
enableReportException(bool $enableReportException)

Enable or disable exception reporting in coroutines.

static void
flushState()

Flush all static state.

static void
printLog(Throwable $throwable)

Report an exception through the exception handler.

Details

at line 29
static int id()

Returns the current coroutine ID.

Returns -1 when running in non-coroutine context.

Return Value

int

at line 40
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.

Parameters

callable $callback

Return Value

void

at line 48
static void defer(callable $callable)

Register a callback to be executed when the coroutine exits.

Parameters

callable $callable

Return Value

void

at line 62
static void sleep(float $seconds)

Sleep for the given number of seconds.

Parameters

float $seconds

Return Value

void

at line 75
static int parentId(int|null $coroutineId = null)

Returns the parent coroutine ID.

Returns 0 when running in the top level coroutine.

Parameters

int|null $coroutineId

Return Value

int

Exceptions

RunningInNonCoroutineException
CoroutineDestroyedException

at line 86
static int pid(int|null $coroutineId = null)

Alias of Coroutine::parentId().

Parameters

int|null $coroutineId

Return Value

int

Exceptions

RunningInNonCoroutineException
CoroutineDestroyedException

at line 94
static int create(callable $callable)

Create a new coroutine.

Parameters

callable $callable

Return Value

int

at line 120
static int fork(callable $callable, array $keys = [])

Create a coroutine with a copy of the parent coroutine context.

Parameters

callable $callable
array $keys

Context keys to copy (empty = all keys)

Return Value

int

at line 139
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.

Parameters

array $coroutineIds
float $timeout

Return Value

bool

at line 147
static bool inCoroutine()

Determine if currently running in a coroutine.

Return Value

bool

at line 155
static array stats()

Get coroutine statistics.

Return Value

array

at line 163
static bool exists(int $id)

Determine if a coroutine with the given ID exists.

Parameters

int $id

Return Value

bool

at line 173
static iterable list()

Get a list of all coroutine IDs.

Return Value

iterable

at line 184
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.

Parameters

bool $enableReportException

Return Value

void

at line 192
static void flushState()

Flush all static state.

Return Value

void

at line 201
static protected void printLog(Throwable $throwable)

Report an exception through the exception handler.

Parameters

Throwable $throwable

Return Value

void