How to Install and Use webman‑coroutine for High‑Performance PHP Services
This guide explains how to install the webman‑coroutine plugin, configure it with workerman, swow or swoole, create a coroutine‑enabled controller, and run concurrent tasks in a PHP webman application, complete with sample commands, code snippets, and execution results.
Introduction
webman‑coroutine is a coroutine infrastructure plugin for the Webman development framework, providing asynchronous capabilities for PHP web services.
Key Features
Supports Workerman 4.x with Swow coroutine driver and is compatible with Workerman 5.x built‑in Swow.
Supports Workerman 4.x with Swoole coroutine driver and is compatible with Workerman 5.x built‑in Swoole.
Supports Ripple coroutine driver and is compatible with the Revolt (PHP‑fiber) ecosystem.
Provides a coroutine web server for building coroutine‑enabled web services.
Works in pure Workerman environments and integrates with the Webman framework.
Installation via Composer
composer require workbunny/webman-coroutineRunning the command updates the lock file, installs dependencies (including swow/swow and workbunny/webman-coroutine), and generates the plugin configuration.
Installing the Swow Extension
Use the Swow builder to compile the extension, ensuring the Swoole environment is disabled.
# ./vendor/bin/swow-builder
cd /var/www/demo.webman.tinywan.com/vendor/swow/swow/ext && \
phpize && \
./configure
make installWebman Configuration
In server.php, set the event loop to automatically select the appropriate driver:
'event_loop' => \Workbunny\WebmanCoroutine\event_loop(),Starting Webman
Run the server with the Swow extension enabled: php -d extension=swow webman start The startup log shows the Workerman version, PHP version, selected event loop, and the list of workers, including the coroutine web server listening on port 8717.
Creating a Coroutine Controller
Example CoroutineController.php demonstrates using WaitGroup and Coroutine to run three tasks concurrently and measure total execution time.
<?php
declare(strict_types=1);
namespace app\controller;
use support\Request;
use Webman\Http\Response;
use Workbunny\WebmanCoroutine\Utils\Coroutine\Coroutine;
use Workbunny\WebmanCoroutine\Utils\WaitGroup\WaitGroup;
class CoroutineController {
public function index(Request $request): Response {
$timeOne = microtime(true);
$waitGroup = new WaitGroup();
// Coroutine 1
$waitGroup->add();
$coroutine1 = new Coroutine(function() use ($waitGroup) {
task1();
$waitGroup->done();
});
// Coroutine 2
$waitGroup->add();
$coroutine2 = new Coroutine(function() use ($waitGroup) {
task2();
$waitGroup->done();
});
// Coroutine 3
$waitGroup->add();
$coroutine3 = new Coroutine(function() use ($waitGroup) {
task3();
$waitGroup->done();
});
$waitGroup->wait();
$timeTwo = microtime(true);
$totalTime = $timeTwo - $timeOne;
echo '[x] [总执行时间] ' . $totalTime . PHP_EOL;
return json(['总执行时间' => $totalTime]);
}
}Task Implementations
/** @desc Task 1 */
function task1(): void {
for ($i = 1; $i <= 1; $i++) {
sleep(1);
echo "[x] [🕷️] [写入文件] [$i] " . date('Y-m-d H:i:s') . PHP_EOL;
}
}
/** @desc Task 2 */
function task2(): void {
for ($i = 1; $i <= 3; $i++) {
sleep(1);
echo "[x] [🍁] [发送邮件] [$i] " . date('Y-m-d H:i:s') . PHP_EOL;
}
}
/** @desc Task 3 */
function task3(): void {
for ($i = 1; $i <= 5; $i++) {
sleep(1);
echo "[x] [🌾] [发送短信] [$i] " . date('Y-m-d H:i:s') . PHP_EOL;
}
}Executing the HTTP Request
Send a GET request to http://127.0.0.1:8217/coroutine/index. The console output shows timestamps for each simulated operation and the total execution time (approximately 5 seconds).
[x] [🕷️] [写入文件] [1] 2024-10-08 23:48:56
[x] [写入文件-总时间] 0.9472222328186
[x] [🍁] [发送邮件] [1] 2024-10-08 23:48:56
[x] [🌾] [发送短信] [1] 2024-10-08 23:48:56
... (subsequent iterations) ...
[x] [发送短信-总时间] 4.9520699977875
[x] [总执行时间] 4.9987840652466Illustration
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.
