$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 = $connection;
echo "连接成功";
$GLOBALS = Timer::add(0.001,array('DealData','dealSream'),array($id, $connection),true);//隔1ms处理一次stream
};
$worker -> onMessage = function($connection,$datas){
//#考虑校验
$id = $connection -> id;
if(!isset($GLOBALS)){
$GLOBALS = $datas;
}else{
$GLOBALS .= $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 = 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 = $connection;
echo "连接成功";
};
$worker -> onMessage = function($connection,$datas){
//#考虑校验
$id = $connection -> id;
if(!isset($GLOBALS)){
$GLOBALS = $datas;
}else{
$GLOBALS .= $datas;//接收流到缓冲区
}
};
当前进程的定时器没执行完,当前进程的onMessage会排队等待,不会被触发。
你可以单独new 一个Worker处理定时任务,比如
$task = new Worker();
$task->onWorkerStart = function () {
Timer::add(...
};
$worker = new Worker('tcp://0.0.0.0:9090');
$worker...
如果这样的话又涉及到进程之间的通讯了吧。
如果onMessage一直排队等待着,而客户端还源源不断的发送数据流会有什么现象?老数据会被新数据替换吗,会有哪个回调函数被触发吗
同一个连接源源不断发送数据,服务端无法及时处理,这些数据会堆积在客户端socket缓冲区,缓冲区满后客户端无法再发送数据。不会有老数据替换新数据的情况
@1:发现问题,定时器一次接一次执行,不给时间去处理收到的数据,怎么让定时器的优先级小于处理端口收到的数据?效果:先处理完数据再去判断是否执行下一次定时器