fiber-interop

fiber\service()

Start a task that does not count towards the enclosing run().

function fiber\service(callable $f, mixed ...$args): object

Behaviour

For process-wide helpers such as a curl poller. It runs in the root scope and must be called inside run() (NotInRunException otherwise). It is driven only while some run() is active; what happens to it after the outermost run() returns is up to the runtime, so write services to be restartable. Its failure goes to the adapter's unhandled-error handler.

Per adapter: phasync cancels services when the outermost run ends; Swoole delivers a Cancelled exception (implementing CancelledException) into the service's waits, and a service that catches it and keeps going would keep Co\run() open (sources: the adapter READMEs).

Example

Source: examples/api/service.php. Run on phasync and Revolt; the output is identical.

<?php
require __DIR__ . '/vendor/autoload.php';
use function fiber\{run, service, sleep};
run(function () {
    service(function () { while (true) { sleep(0.05); } }); // does not hold up run()
    sleep(0.1);
    echo "run() ends although the service loops\n";
});

All functions