您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman接口stopAll及多线程请求测试
发布时间:2021-12-02 22:41:07编辑:雪饮阅读()
stopAll
void Worker::stopAll(void)
停止当前进程(子进程)的所有Worker实例并退出。
此方法用于安全退出当前子进程,作用相当于调用exit/die退出当前子进程。
与直接调用exit/die区别是,直接调用exit或者die无法触发onWorkerStop回调,并且会导致一条WORKER EXIT UNEXPECTED错误日志。
参数
无参数
返回值
无返回
范例 max_request
下面例子子进程每处理完5个请求后执行stopAll退出,以便重新启动一个全新进程。类似php-fpm的max_request属性,主要用于解决php业务代码bug引起的内存泄露问题。
范例实现;
test.php:
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
// 每个进程最多执行5个请求
define('MAX_REQUEST', 5);
$http_worker = new Worker("http://0.0.0.0:8484");
$http_worker->onMessage = function(TcpConnection $connection, $data)
{
// 已经处理请求数
static $request_count = 0;
$connection->send('hello http');
// 如果请求数达到5
if(++$request_count >= MAX_REQUEST)
{
/*
* 退出当前进程,主进程会立刻重新启动一个全新进程补充上来
* 从而完成进程重启
*/
var_dump("进程重启了!");
Worker::stopAll();
}
};
Worker::runAll();
?>
测试客户端环境准备:
php7 ts
https://phpdev.toolsforresearch.com/php-7.0.33-Win32-VC14-x86.zip
php.ini注意不要用php.ini.bk,有坑
php7 ts 多线程扩展,目前最高支持7.0的ts版
https://windows.php.net/downloads/pecl/releases/pthreads/3.1.6/php_pthreads-3.1.6-7.0-ts-vc14-x86.zip
扩展安装参考:
https://www.gaojiupan.cn/manshenghuo/chengxurensheng/4009.html
扩展安装完成就可以执行如下命令在回显结果中应该包含有pthreads
C:\phpstudy_pro\Extensions\php\php-7.0.33-Win32-VC14-x86>php -i -c "C:\phpstudy_pro\Extensions\php\php-7.0.33-Win32-VC14-x86\php.ini" | findstr /i "Thread"
Thread Safety => enabled
pthreads
客户端测试脚本test2.php:
<?php
class OptimisticLock extends Thread {
public function run() {
$content=file_get_contents("http://47.240.19.5:8484");
echo "\r\n".$content."\r\n";
}
}
$clientArr = [];
for ($i=0;$i<10;++$i) {
$clientArr[$i] = new OptimisticLock();
$clientArr[$i]->start();
}
?>
服务端运行并有客户端脚本运行时:
[root@izj6c2jeancylo0ppo4vz5z workerman]# php test.php start
Workerman[test.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.22 PHP version:7.0.33
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root none http://0.0.0.0:8484 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
string(16) "进程重启了!"
客户端脚本运行:
C:\phpstudy_pro\Extensions\php\php-7.0.33-Win32-VC14-x86>php -c C:\phpstudy_pro\Extensions\php\php-7.0.33-Win32-VC14-x86\php.ini C:\phpstudy_pro\WWW\www.zzff.com\test2.php
hello http
PHP Warning: file_get_contents(http://47.240.19.5:8484): failed to open stream: HTTP request failed! in C:\phpstudy_pro\W
hello http
W
hello http
W\w
hello http
ww.zzff.com\test2.php on line 4
hello http
P
Warning: file_get_contents(http://47.240.19.5:8484): failed to open stream: HTTP request failed! in C:\phpstudy_pro\WWW\www.zzff.com\test2.php on line 4
HP Warning: file_get_c
ontents(http://47.240.19.5:8484): failed to open stream: HTTP request failed! in C:\phpstudy_pro\WWW\www.zzff.com\test2.php on line 4
PH
Warning: file_get_contents(http://47.240.19.5:8484): failed to open stream: HTTP request failed! in C:\phpstudy_pro\WWW\www.zzff.com\test2.php on line 4
P Warning:
file_get_contents(http://47.240.19.5:8484): failed to open stream: HTTP request failed! in C:\phpstudy_pro\WWW\www.zzff.com\test2.php on line 4
Warning: file_get_contents(http://47.240.19.5:8484): failed to open stream: HTTP request failed! in C:\phpstudy_pro\WWW\www.zzff.com\test2.php on line 4
PHP Warning:
file_get_contents(http://47.240.19.5:8484): failed to open stream: HTTP request failed! in C:\phpstudy_pro\WWW\www.zzff.com\test2.php on line 4
PHP Warning: file_get_contents(http://47.240.19.5:8484): failed to o
Warning: file_get_contents(http://47.240.19.5:8484): failed to open stream: HTTP request failed! in C:\phpstudy_pro\WWW\www.zzff.com\test2.php on line 4
pen stream: HTTP request failed! in C:\phpstudy_pro\WWW\www.zzff.co
m\test2.php on line 4
Warning: file_get_contents(http://47.240.19.5:8484): failed to open stream: HTTP request failed! in C:\phpstudy_pro\WWW\www.zzff.com\test2.php on line 4
这里的错误是正常的,重要的是服务端有提示“进程重启了!”
关键字词:workerman,接口,stopAll,多线程
相关文章
- workerman利用Timer库的persistent属性实现仅定时执行
- workerman利用daemonize属性实现代码层控制进程启动方
- workerman利用reloadable属性实现新增业务代码载入并
- workerman属性stdoutFile(输出重定向)及daemonize实现脚
- workerman利用属性user实现指定用户运行进程
- workerman利用属性pidFile自定义进程pid文件
- workerman利用属性connections遍历向客户端定时发消息
- workerman利用logFile属性指定自身运行日志存储路径(不
- workerman端口复用reusePort属性实现同一个入口监听多
- workerman端口复用reusePort属性