Step‑by‑Step Guide to Installing and Using Swow Coroutine Engine with PHP Webman
This tutorial explains what Swow is, walks through installing Workerman 5.0 and the Swow extension via Composer, shows how to run the Swow builder, configure Webman to use Swow's event loop, and demonstrates basic coroutine usage and debugging in PHP.
What is Swow?
Swow is a cross‑platform coroutine engine focused on concurrent I/O, built with a minimal C core and mostly PHP code to enable high‑performance network programming in PHP. It offers strong extensibility and debugging capabilities to maximize developer productivity.
Install Workerman 5.0
composer require workerman/workerman v5.0.0-beta.7The command updates composer.json and installs the specified Workerman version.
Install the Swow extension
composer require swow/swowComposer resolves dependencies and installs Swow version ^1.5 (currently 1.5.3).
Run the Swow builder
Execute the automatic installer provided by Swow: ./vendor/bin/swow-builder The builder runs phpize, configures the extension, and compiles it. After successful compilation you will see messages confirming installation of the shared extension.
Enable Swow event loop in Webman
Modify config/server.php to use Swow as the event loop:
return [
'listen' => 'http://0.0.0.0:8787',
'transport' => 'tcp',
'event_loop' => \Workerman\Events\Swow::class,
// other settings omitted for brevity
];Start the server
Run Webman with the Swow extension loaded: php -d extension=swow webman start The console displays Workerman version, PHP version, and confirms that the Swow event loop is active. You can check the running status with: php start.php status An example screenshot of the status page is shown below.
Coroutines in Swow
Coroutines are lightweight, user‑space constructs that are managed by the program rather than the OS kernel. Swow provides a simple API to create, control, and debug coroutines.
<?php
declare(strict_types=1);
use Swow\Coroutine;
$coroutine = Coroutine::run(static function(): void {
while (true) {
try {
// output: int(999)
var_dump(sleep(999));
} catch (\Exception) {
// skip
}
}
});
$coroutine->resume(); // cancel the sleep
$coroutine->throw(new Exception()); // throw exception into coroutine
$coroutine->kill(); // terminate coroutine (like SIGKILL)Debug mode
Swow also offers a debug mode to inspect coroutine state:
\Swow\Coroutine::run(static function(): void {
(static function(): void {
(static function(): void {
var_dump(\Swow\Coroutine::getCurrent());
})();
})();
});The output shows detailed information such as coroutine ID, state, switch count, elapsed time, and a stack trace.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
