Driver

Flow embark multiple drivers.

Coroutine

Drivers are useful to essentially provide asynchronous programming by using coroutines. Thus, this can be implemented in severals ways in most popular programming languages.

Coroutine are very similar to threads and provide concurrency but not parallelism. Advantage of using coroutine :

  • this can be a preferred usage to thread for hard-realtime context.
  • there is no need for synchronisation primitives such as mutexes, semaphore.
  • it reduces the usage of system lock for sharing resources.

Amp Driver

To use Amp Driver, you have to require the library with composer

composer require amphp/amp

More documentation can be found https://amphp.org

Fiber Driver

More documentation can be found https://www.php.net/manual/fr/language.fibers.php

ReactPHP Driver

To use ReactPHP Driver, you have to require the library with composer

composer require react/async

More documentation can be found https://reactphp.org

Spatie Driver

To use Spatie Driver, you have to require the library with composer

composer require spatie/async

More documentation can be found https://github.com/spatie/async

Swoole Driver

To use Swoole Driver, you have to add the extension with your current running PHP

pecl install openswoole-22.0.0

More documentation can be found https://openswoole.com

Parallel Driver

To use Parallel Driver, you have to require the library with PECL

pecl install parallel

More documentation can be found https://www.php.net/manual/en/book.parallel.php

StreamSelect Driver

Native PHP Streams driver: cooperative Generators + stream_select().

Jobs that need overlapping I/O must be Generators and yield wait tokens (Fibers are not used):

use Flow\Driver\StreamSelectDriver;

$driver = new StreamSelectDriver();

// Inside a Generator job:
$ready = (yield $driver->waitReadable($stream, timeoutSeconds: 5.0));
$ready = (yield $driver->waitWritable($stream, timeoutSeconds: 5.0));
yield $driver->waitDelay(0.05);

stream_set_blocking() + stream_select() are the long-standing PHP async I/O primitives. The Driver only multiplexes readiness — HTTP, hashing, and persistence stay in Jobs. Pair with MaxIpStrategy($n) so at most $n IPs (and their stream waits) are in flight at a stage.

FiberDriver remains the default. Future PHP stream polling improvements (epoll/kqueue) may replace the mux without changing the wait-token API.

TrueAsync Driver

Experimental driver backed by the TrueAsync ext-async extension. FiberDriver remains the default.

Requires a custom PHP 8.6+ build with the extension enabled. Composer suggests it as an optional dependency:

# ext-async is not installable via Composer — build PHP with the TrueAsync fork
# See https://true-async.github.io/download.html

Check availability before instantiating the driver:

use Flow\Driver\TrueAsyncDriver;

if (TrueAsyncDriver::isSupported()) {
    $driver = new TrueAsyncDriver();
}

Usage:

use Flow\Driver\TrueAsyncDriver;
use Flow\Flow\Flow;
use Flow\Ip;

$flow = (new Flow(
    job: fn (int $n) => $n * 2,
    driver: new TrueAsyncDriver(),
))->fn(fn (int $n) => $n + 1);

$flow(new Ip(21));
$flow->await();

Do not mix TrueAsyncDriver with FiberDriver in the same process — TrueAsync blocks userland Fiber while active.

A working example is available: php examples/flow-true-async.php

More documentation can be found https://true-async.github.io

Make your custom driver

You can make your custom driver by implementing Flow\DriverInterface

Edit this page on GitHub