windows下异步任务执行还是在一个进程中

dennlian

客户端请求

use Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;
require 'vendor/autoload.php';
$worker = new Worker();
$worker->onWorkerStart = function(){
    for($i=0;$i<10;$i++){
        $task_connection = new AsyncTcpConnection('Text://192.168.1.128:12345');
// 任务及参数数据
        $task_data = array(
            'function' => 'send_mail',
            'args'       => array('from'=>'xxx', 'to'=>'xxx', 'contents'=>'xxx'),
        );
// 发送数据

        $task_connection->onConnect = function($task_connection) use($task_data){
            echo 2;
            $task_connection->send(json_encode($task_data));
        };
// 异步获得结果
        $task_connection->onMessage = function($task_connection, $task_result){
            // 结果
            // 获得结果后记得关闭异步连接
            $task_connection->close();
            // 通知对应的websocket客户端任务完成
        };
        $task_connection->onError = function(){
            echo 6;
        };

// 执行异步连接
        $task_connection->connect();
    }

};

Worker::runAll();

服务端代码

use Workerman\Worker;
require_once 'vendor/autoload.php';
// task worker,使用Text协议
$task_worker = new Worker('Text://0.0.0.0:12345');
// task进程数可以根据需要多开一些
$task_worker->count = 100;
$task_worker->name = 'TaskWorker';
//只有php7才支持task->reusePort,可以让每个task进程均衡的接收任务
$task_worker->reusePort = true;
$task_worker->onMessage = function($connection, $task_data)
{
    // 假设发来的是json数据
    $task_data = json_decode($task_data, true);
    echo 1;
    sleep(10);//模拟进程阻塞
    $connection->send(json_encode($task_data));
};
Worker::runAll();

运行结果服务端还是在一个进程中运行

2830 1 0
1个回答

walkor

设置下
$task_worker->reusePort = true;

这个选项在php7下才有效

  • dennlian 2018-06-21

    感谢回复,我设置了的。我看了下文档说windows下不支持多进程,$task_worker->count = 100属性设置没用,应该是这个原因造成的

  • walkor 2018-06-21

    是的,windows不支持进程数设置,是这个原因造成的

  • William_chen 2021-04-23

    linux就可以多进程了吗?

  • walkor 2021-04-24

    可以

年代过于久远,无法发表回答
🔝