父进程与子进程间如何互相调用参数

素年锦时
$httpserver = new Worker('websocket://0.0.0.0:1000');
$httpserver->onMessage = function($conhttp,$msg)use($connection)
{
    print_r($msg);
};
$httpserver->onWorkerStart = function($conhttp)
{
    $connection = new AsyncTcpConnection('tcp://10.10.10.200:8021');
        $connection->onMessage = function($connection,$buffer)use($conhttp)
        { 
                  //如何在此处获取到$msg的值
        };
};
2946 1 0
1个回答

phpcreeper

首先你这也不是父子进程之间的通信,onXXX回调方法里的都是子进程逻辑。
AsyncTcpConnection(8021) 啥意思? 应该是AsyncTcpConnection(1000),参考代码【不过只适用于单进程模型】:

<?php
use Workerman\Worker;
use Workerman\Lib\Timer;
use Workerman\Connection\AsyncTcpConnection;
require_once dirname(__DIR__) . '/Workerman/Autoloader.php';

$worker = new Worker('text://0.0.0.0:3000');
$worker->count = 1;

$worker->onWorkerStart = function($worker) {
    $connection = new AsyncTcpConnection('text://127.0.0.1:3000');
    $connection->connect();
    $connection->onMessage = function($connection, $msg){
        pprint($msg);
    };  
}; 

$worker->onMessage = function($connection, $msg){
    foreach($connection->worker->connections as $one_connection){
        $one_connection->send($msg);
    }   
};

Worker::runAll();           

对于多进程之间的通信,有很多方法:
(1)使用官方提供的GlobalData组件 或者 Channle组件。
(2)使用 redis、memcache 或者 mysql 等。
(3)使用类似官方的GatewayWorker通信模型,即:注册一个全局register进程,$httpserver进程onWorkerStart时连接register进程,这样当不同进程的客户端通信时,交由register进程来转发数据。

  • 暂无评论
年代过于久远,无法发表回答
×
🔝