Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-26.2.0 | composer require openswoole/core:26.2.0
Since: OpenSwoole v26.2.0
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.
| Constant | Platform | Description |
|---|---|---|
OPENSWOOLE_EPOLL | Linux | Default on Linux. High-performance event polling. |
OPENSWOOLE_KQUEUE | macOS/BSD | Default on macOS and BSD systems. |
OPENSWOOLE_POLL | All | Portable poll() backend. Works on all platforms. |
OPENSWOOLE_SELECT | All | Portable select() backend. Limited to 1024 file descriptors. |
OPENSWOOLE_IO_URING | Linux 5.1+ | High-performance io_uring backend. Requires --enable-io-uring and liburing. |
<?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.
<?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();
<?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();
<?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";
}
--enable-io-uring compile flag with liburing installedOPENSWOOLE_KQUEUE is used by default; attempting to use OPENSWOOLE_EPOLL will fail