定时器是否会影响收数据

$worker = new Worker('tcp://0.0.0.0:9090'); //http协议
//启动1个进程对外提供服务
$worker -> count = 1;
Worker::$daemonize = true;
Worker::$stdoutFile = '/tmp/stdout.log';
//增加一个属性
$worker -> uidConnections = array();
$worker -> onWorkerStart = function($worker){
$worker -> onConnect = function($connection){
global $worker;
$ip = $connection -> getRemoteIp();
$id = $connection -> id;
$worker -> uidConnections[$ip] = $connection;
echo "连接成功";
$GLOBALS['DealStream_timeId'][$id] = Timer::add(0.001,array('DealData','dealSream'),array($id, $connection),true);//隔1ms处理一次stream
};
$worker -> onMessage = function($connection,$datas){
//#考虑校验
$id = $connection -> id;
if(!isset($GLOBALS['stream'][$id])){
$GLOBALS['stream'][$id] = $datas;
}else{
$GLOBALS['stream'][$id] .= $datas;//接收流到缓冲区
}
};
比如这段程序,
1.如果我的定时器那边还没执行完的情况下,onmessage区间是否还能正常接收存储数据?
2.如果我设置进程数是2或者更多,是系统自动选择进行执行,还是需要手动规定某个进程执行哪些业务,比如if($worker -> id ==0){第一个进程}else{其他进程业务}?
3.‘1’问题中定时器会影响收数据,那我能不能开两个进程,一个进程专门运行定时器处理数据,另一个进程做目前的其他业务。改成这样的代码
$worker = new Worker('tcp://0.0.0.0:9090'); //http协议
//启动1个进程对外提供服务
$worker -> count = 2;
Worker::$daemonize = true;
Worker::$stdoutFile = '/tmp/stdout.log';
//增加一个属性
$worker -> uidConnections = array();
$worker -> onWorkerStart = function($worker){
if($worker -> id == 0){
$GLOBALS['DealStream_timeId'][$id] = Timer::add(0.001,array('DealData','dealSream'),array($id, $connection),true);//隔1ms处理一次stream
}else{
$worker -> onConnect = function($connection){
global $worker;
$ip = $connection -> getRemoteIp();
$id = $connection -> id;
$worker -> uidConnections[$ip] = $connection;
echo "连接成功";

};
$worker -> onMessage = function($connection,$datas){
//#考虑校验
$id = $connection -> id;
if(!isset($GLOBALS['stream'][$id])){
$GLOBALS['stream'][$id] = $datas;
}else{
$GLOBALS['stream'][$id] .= $datas;//接收流到缓冲区
}
};
已邀请:

walkor

赞同来自: 隋意

当前进程的定时器没执行完,当前进程的onMessage会排队等待,不会被触发。
你可以单独new 一个Worker处理定时任务,比如
 
$task = new Worker();
$task->onWorkerStart = function () {
    Timer::add(...
};
 
$worker = new Worker('tcp://0.0.0.0:9090');
$worker...

要回复问题请先登录注册