Master Swoole: Install, Configure, and Harness PHP Coroutines

This guide explains what Swoole is, provides step‑by‑step installation and configuration instructions for the PHP extension, shows how to enable it in the Webman framework, and demonstrates coroutine concepts with practical PHP code examples and expected output.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Master Swoole: Install, Configure, and Harness PHP Coroutines

What Is Swoole?

Swoole is a high‑performance, asynchronous, event‑driven network communication engine written in C++. It extends PHP with coroutine support and enables fast TCP/UDP, HTTP, WebSocket, IoT, real‑time communication, gaming, and micro‑service capabilities, moving PHP beyond traditional web development.

Installing the Swoole Extension

The extension follows the standard PHP extension build process. Use phpize to generate the build script, then run ./configure, make, and make install.

wget https://pecl.php.net/get/swoole-5.1.1.tgz

tar -zxvf swoole-5.1.1.tgz

cd swoole-5.1.1
/usr/local/php-8.2.14/bin/phpize
./configure --with-php-config=/usr/local/php-8.2.14/bin/php-config
make
sudo make install

After compilation, add the extension to php.ini: extension=swoole.so Verify the installation with:

/usr/local/php-8.2.14/bin/php -m | grep swoole

Enabling Swoole in Webman

Modify config/server.php to use the Swoole event loop:

return [
    'listen'        => 'http://0.0.0.0:8787',
    'transport'     => 'tcp',
    'event_loop'    => Workerman\Events\Swoole::class,
    // other settings omitted for brevity
];

Start the server: /usr/local/php-8.2.14/bin/php start.php start Check the running status with:

/usr/local/php-8.2.14/bin/php start.php status

Optional: Install IDE Helper

composer require --dev swoole/ide-helper:@dev

Understanding Coroutines

Coroutines are lightweight, user‑space constructs that allow concurrent execution without OS‑level threads. They are managed by the program itself, making context switches fast and efficient, especially for I/O‑bound tasks.

Example illustrating asynchronous execution of three tasks (boiling water, washing face, eating breakfast):

<?php
// Task 1 – boil water (10 s)
go(function () {
    \Co::sleep(10);
    echo "[x] [Task1] Boil water – 10s
";
});

// Task 2 – wash face (3 s)
go(function () {
    \Co::sleep(3);
    echo "[x] [Task2] Wash face – 3s
";
});

// Task 3 – eat breakfast (5 s)
go(function () {
    \Co::sleep(5);
    echo "[x] [Task3] Eat breakfast – 5s
";
});

echo "[x] Main task start
";
?>

Running the script produces interleaved output, showing that while the 10‑second water‑boiling I/O is pending, the shorter tasks complete first, demonstrating the time‑saving benefit of coroutines.

Key Takeaways

Swoole brings asynchronous, coroutine‑based networking to PHP.

Installation requires compiling the extension and updating php.ini.

Webman can be configured to use Swoole’s event loop with a single setting.

Coroutines enable non‑blocking I/O, allowing multiple logical tasks to run concurrently within a single process.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendcoroutineSwoole
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.