Deprecated
: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in
/www/wwwroot/testblog.58heshihu.com/var/Widget/Archive.php
on line
1057
首页
关于
Search
1
给你10个市场数据调研报告的免费下载网站!以后竞品数据就从这里找!
182 阅读
2
php接口优化 使用curl_multi_init批量请求
144 阅读
3
《从菜鸟到大师之路 ElasticSearch 篇》
107 阅读
4
2024年备考系统架构设计师
104 阅读
5
PHP 文件I/O
92 阅读
php
thinkphp
laravel
工具
开源
mysql
数据结构
总结
思维逻辑
令人感动的创富故事
读书笔记
前端
vue
js
css
书籍
开源之旅
架构
消息队列
docker
教程
代码片段
redis
服务器
nginx
linux
科普
java
c
ElasticSearch
测试
php进阶
php基础
登录
Search
标签搜索
php函数
php语法
性能优化
安全
错误和异常处理
问题
vue
Composer
Session
缓存
框架
Swoole
api
并发
异步
正则表达式
php-fpm
mysql 索引
开发规范
协程
dafenqi
累计撰写
786
篇文章
累计收到
28
条评论
首页
栏目
php
thinkphp
laravel
工具
开源
mysql
数据结构
总结
思维逻辑
令人感动的创富故事
读书笔记
前端
vue
js
css
书籍
开源之旅
架构
消息队列
docker
教程
代码片段
副业
redis
服务器
nginx
linux
科普
java
c
ElasticSearch
测试
php进阶
php基础
页面
关于
搜索到
1
篇与
的结果
2023-08-08
php并行运行多任务,daemon守护进程
php并行运行多任务,daemon守护进程利用swoole框架,适合运行并行的长时间(一直运行)的任务.如下<?php $redirect_stdout = false; $workers = []; $worker_num = 8; cli_set_process_title('monitormain');//修改进程名,need php>5.5 //swoole_process::daemon(0, 1);/yuan swoole_process::daemon(0, 0);//as a daemon for($i = 0; $i < $worker_num; $i++) { $process = new swoole_process('child_async', $redirect_stdout);//创建子进程 $pid = $process->start();//子进程pid $workers[$pid] = $process; } master_async($workers); //异步主进程,用于与子进程通信等 function master_async($workers) { swoole_process::signal(SIGCHLD, function ($signo) use ($workers) { $ret = swoole_process::wait(); $pid = $ret['pid']; unset($workers[$pid]); echo "Worker Exit, PID=" . $pid . PHP_EOL; }); /** * @var $process swoole_process */ /*与子进程通信 foreach($workers as $pid => $process) { swoole_event_add($process->pipe, function($pipe) use ($process) { $recv = $process->read(); if ($recv) echo "From Worker: " . $recv; }); $process->write("hello worker[$pid]\n"); }*/ } function child_async(swoole_process $worker) { //echo "Worker: start. PID=".$worker->pid."\n"; //recv data from master $GLOBALS['worker'] = $worker; global $argv; //$worker->name("{$argv[0]}: worker");//修改进程名 $worker->name("monitor");//修改进程名 swoole_process::signal(SIGTERM, function($signal_num) use ($worker) { echo "signal call = $signal_num, #{$worker->pid}\n"; }); //do something echo 'hello'; /*异步,同步时不需要 swoole_event_add($worker->pipe, function($pipe) use($worker) { $recv = $worker->read(); echo "From Master: $recv\n"; $worker->write("hello master\n"); });*/ }---------------------------------以下是完整代码----------------------<?php //php并行执行多个任务 //用于监控集群工作状态 //用法:终端执行:php monitor.php Monitor node 192.168.6.157 192.168.6.156.... $redirect_stdout = false;//重定向子进程的标准输入和输出 $workers = []; isset($argv)?$worker_num = count($argv)-3:1;//$worker_num:子进程数 cli_set_process_title('monitormain');//修改进程名,need php>=5.5 swoole_process::daemon(1, 0);//as a daemon //循环创建子进程 for($i = 0; $i < $worker_num; $i++) { $process = new swoole_process('child_async', $redirect_stdout);//创建子进程 $pid = $process->start();//子进程pid $workers[$pid] = $process; } master_async($workers);//执行主进程 //异步主进程,用于与子进程通信等 function master_async($workers) { global $argv; $argnum=2; swoole_process::signal(SIGCHLD, function ($signo) use ($workers) { $ret = swoole_process::wait(); $pid = $ret['pid']; unset($workers[$pid]); echo "Worker Exit, PID=" . $pid . PHP_EOL; }); foreach($workers as $pid => $process) { $argnum++; swoole_event_add($process->pipe, function($pipe) use ($process) { //$recv = $process->read();//从子进程读消息 if ($recv) echo "From Worker: " . $recv; }); $process->write("$argv[$argnum]");//向子进程写消息 } } //异步子进程 function child_async(swoole_process $worker) { $GLOBALS['worker'] = $worker; global $argv; $worker->name("monitor");//修改进程名 swoole_process::signal(SIGTERM, function($signal_num) use ($worker) { echo "signal call = $signal_num, #{$worker->pid}\n"; }); //执行任务 swoole_event_add($worker->pipe, function($pipe) use($worker,$argv) { $recv = $worker->read();//读取主进程消息 //echo "From Master: $recv ***\n"; //$worker->write("hello master\n");//向主进程写消息 $args= array('cli.php', $argv[1],$argv[2],$recv); $worker->exec('/usr/bin/php',$args);//执行命令 }); }
2023年08月08日
13 阅读
0 评论
0 点赞