Coroutine Reactor Type Selection

Latest version: pecl install openswoole-26.2.0 | composer require openswoole/core:26.2.0

Since: OpenSwoole v26.2.0

Description

OpenSwoole 26.2.0 allows you to select the event reactor backend at runtime via Co::set(). Previously, the reactor type was determined at compile time. Now you can choose the best backend for your platform and workload.

Available Reactor Types

ConstantPlatformDescription
OPENSWOOLE_EPOLLLinuxDefault on Linux. High-performance event polling.
OPENSWOOLE_KQUEUEmacOS/BSDDefault on macOS and BSD systems.
OPENSWOOLE_POLLAllPortable poll() backend. Works on all platforms.
OPENSWOOLE_SELECTAllPortable select() backend. Limited to 1024 file descriptors.
OPENSWOOLE_IO_URINGLinux 5.1+High-performance io_uring backend. Requires --enable-io-uring and liburing.

Configuration

<?php
OpenSwoole\Coroutine::set([
    'reactor_type' => OPENSWOOLE_IO_URING,
]);

If the selected reactor type is not available on the current platform, OpenSwoole will throw an error at runtime.

Example: Using epoll (Linux Default)

<?php
use OpenSwoole\Coroutine;
use OpenSwoole\Http\Server;

Coroutine::set([
    'reactor_type' => OPENSWOOLE_EPOLL,
]);

$server = new Server("0.0.0.0", 9501);

$server->on("request", function ($request, $response) {
    $response->end("Hello from epoll reactor\n");
});

$server->start();

Example: Using io_uring for High-Performance I/O

<?php
use OpenSwoole\Coroutine;
use OpenSwoole\Http\Server;

// io_uring provides better performance for high-throughput workloads
Coroutine::set([
    'reactor_type' => OPENSWOOLE_IO_URING,
    'hook_flags' => OpenSwoole\Runtime::HOOK_ALL,
]);

$server = new Server("0.0.0.0", 9501);

$server->set([
    'worker_num' => 4,
]);

$server->on("request", function ($request, $response) {
    // File I/O automatically uses io_uring async engine
    $content = file_get_contents('/var/data/config.json');

    $response->header('Content-Type', 'application/json');
    $response->end($content);
});

$server->start();

Example: Checking Available Reactor Types

<?php
// Check which reactor constants are defined
$reactors = [
    'OPENSWOOLE_EPOLL' => defined('OPENSWOOLE_EPOLL'),
    'OPENSWOOLE_KQUEUE' => defined('OPENSWOOLE_KQUEUE'),
    'OPENSWOOLE_POLL' => defined('OPENSWOOLE_POLL'),
    'OPENSWOOLE_SELECT' => defined('OPENSWOOLE_SELECT'),
    'OPENSWOOLE_IO_URING' => defined('OPENSWOOLE_IO_URING'),
];

foreach ($reactors as $name => $available) {
    echo "$name: " . ($available ? 'available' : 'not available') . "\n";
}

Notes

  • Reactor type constants are always defined regardless of platform; unsupported backends are rejected at runtime
  • io_uring requires Linux kernel 5.1+ and the --enable-io-uring compile flag with liburing installed
  • io_uring supports multishot poll on kernel 5.13+ with automatic fallback to single-shot on older kernels
  • On macOS, OPENSWOOLE_KQUEUE is used by default; attempting to use OPENSWOOLE_EPOLL will fail
Last updated on February 28, 2026