🧨 webman-coroutine协程插件 1.0.0 正式发布
🐞 简介
🚀🚀 webman-coroutine 是一个 webman 开发框架生态下的协程基建支撑插件
🕷️ 功能
- 支持
workerman 4.x
的 swow 协程驱动能力,兼容workerman 5.x
版本自带的swow
协程驱动; - 支持
workerman 4.x
的 swoole 协程驱动能力,兼容workerman 5.x
版本自带的swoole
协程驱动; - 支持 ripple 协程驱动能力,兼容
revolt (PHP-fiber)
协程生态; - 提供
coroutine web server
用于实现具备协程能力的web服务; - 支持纯 workerman 环境,支持 webman 开发框架
🪰 安装
通过composer
安装
composer require workbunny/webman-coroutine
输出日志
/var/www/demo.webman.tinywan.com # composer require workbunny/webman-coroutine
./composer.json has been updated
Running composer update workbunny/webman-coroutine
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
- Locking composer/semver (3.4.3)
- Locking swow/swow (v1.5.3)
- Locking workbunny/webman-coroutine (1.0.2)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
- Installing composer/semver (3.4.3): Extracting archive
- Installing swow/swow (v1.5.3): Extracting archive
- Installing workbunny/webman-coroutine (1.0.2): Extracting archive
> support\Plugin::install
> support\Plugin::install
> support\Plugin::install
Create config/plugin/workbunny/webman-coroutine
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
55 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
Using version ^1.0 for workbunny/webman-coroutine
🪲 安装swow
使用./vendor/bin/swow-builder
安装swow
拓展,注意请关闭swoole
环境
/var/www/demo.webman.tinywan.com # ./vendor/bin/swow-builder
> cd /var/www/demo.webman.tinywan.com/vendor/swow/swow/ext && \
phpize && \
./configure
Configuring for:
PHP Api Version: 20230831
Zend Module Api No: 20230831
Zend Extension Api No: 420230831
checking for grep that handles long lines and -e... /bin/grep
....
👀 Do you want to install it right now? (Y/n): Y
> cd /var/www/demo.webman.tinywan.com/vendor/swow/swow/ext && \
make install
Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20230831/
✅ Install done
🚀🚀🚀 All tasks have been completed 🚀🚀🚀
🐜 webman 使用
1. 配置server.php
修改事件驱动为event_loop()
,event_loop()
用于自动判断当前环境适合的event loop
和协程驱动
'event_loop' => \Workbunny\WebmanCoroutine\event_loop(),
2. 启动webman
通过以下命令启动webman
php -d extension=swow webman start
启动输出
/var/www/demo.webman.tinywan.com # php -d extension=swow webman start
Workerman[webman] start in DEBUG mode
---------------------------------------------------------------- WORKERMAN -----------------------------------------------------------------
Workerman version:4.1.15 PHP version:8.3.9 Event-Loop:Workbunny\WebmanCoroutine\Events\SwowEvent
----------------------------------------------------------------- WORKERS ------------------------------------------------------------------
proto user worker listen processes status
tcp root webman http://0.0.0.0:8217 8 [OK]
tcp root monitor none 1 [OK]
tcp root plugin.workbunny.webman-coroutine.coroutine-web-server http://[::]:8717 2 [OK]
tcp root plugin.saiadmin.task none 1 [OK]
tcp root plugin.saiadmin.websocket websocket://0.0.0.0:9527 1 [OK]
--------------------------------------------------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
3. 新建控制器
CoroutineController.php
伪代码
<?php
/**
* @author Tinywan(ShaoBo Wan)
* @date 2024/10/8 23:37
*/
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();
// 协程1
$waitGroup->add();
$coroutine1 = new Coroutine(function () use ($waitGroup) {
task1();
$waitGroup->done();
});
// 协程2
$waitGroup->add();
$coroutine2 = new Coroutine(function () use ($waitGroup) {
task2();
$waitGroup->done();
});
// 协程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]);
}
}
模拟执行任务方法
/** @desc 任务1 */
function task1(): void
{
$timeOne1 = microtime(true);
for ($i = 1; $i <= 1; $i++) {
sleep(1);
echo '[x] [🕷️] [写入文件] [' . $i . '] ' . date('Y-m-d H:i:s') . PHP_EOL;
}
$timeTwo1 = microtime(true);
echo '[x] [写入文件-总时间] ' . ($timeTwo1 - $timeOne1) . PHP_EOL . PHP_EOL;
}
/** @desc 任务2 */
function task2(): void
{
$timeOne2 = microtime(true);
for ($i = 1; $i <= 3; $i++) {
sleep(1);
echo '[x] [🍁] [发送邮件] [' . $i . '] ' . date('Y-m-d H:i:s') . PHP_EOL;
}
$timeTwo2 = microtime(true);
echo '[x] [发送邮件-总时间] ' . ($timeTwo2 - $timeOne2) . PHP_EOL . PHP_EOL;
}
/** @desc 任务3 */
function task3(): void
{
$timeOne3 = microtime(true);
for ($i = 1; $i <= 5; $i++) {
sleep(1);
echo '[x] [🌾] [发送短信] [' . $i . '] ' . date('Y-m-d H:i:s') . PHP_EOL;
}
$timeTwo3 = microtime(true);
echo '[x] [发送短信-总时间] ' . ($timeTwo3 - $timeOne3) . PHP_EOL . PHP_EOL;
}
4、执行Http请求
请求地址:http://127.0.0.1:8217/coroutine/index
控制台输出结果
[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
[x] [🍁] [发送邮件] [2] 2024-10-08 23:48:57
[x] [🌾] [发送短信] [2] 2024-10-08 23:48:57
[x] [🍁] [发送邮件] [3] 2024-10-08 23:48:58
[x] [发送邮件-总时间] 2.9497091770172
[x] [🌾] [发送短信] [3] 2024-10-08 23:48:58
[x] [🌾] [发送短信] [4] 2024-10-08 23:48:59
[x] [🌾] [发送短信] [5] 2024-10-08 23:49:00
[x] [发送短信-总时间] 4.9520699977875
[x] [总执行时间] 4.9987840652466
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 4个月前 自动加精