10个进程,10个定时器,如何每个进程运行一个定时器?

north521

2、添加的定时任务在当前进程执行(不会启动新的进程或者线程),如果任务很重(特别是涉及到网络IO的任务),可能会导致该进程阻塞,暂时无法处理其它业务。所以最好将耗时的任务放到单独的进程运行,例如建立一个/多个Worker进程运行

if($worker->id === 0)
    {
        Timer::add(1, function(){
            echo "4个worker进程,只在0号进程设置定时器\n";
        });
    }

这个只对进程id为0有效,比如我有10个定时器,开启10个进程,怎么让每个进程运行一个定时器?

3594 3 0
3个回答

抽不完的寂寞

把if($worker->id === 0) 去掉就是每个进程运行一个定时器啊

north521

当前开了10个进程,已经运行5个定时器,在进程0-4运行,我从前端onMessage添加的定时器任务,怎么在进程5运行?

  • 暂无评论
north521
<?php
namespace app\push\controller;
use think\worker\Server;
use Workerman\Lib\Timer;
require_once APP_PATH . '/push/controller/Cron.php';
class Worker extends Server
{
    protected $socket = 'text://0.0.0.0:2346';
    //protected $count = 1;
    protected $processes=10;

    public function onWorkerStart($work){
        //读出符合条件的任务
        $task = new Task();
        $taskList = $task->getTaskList();
        if ($taskList){
            foreach ($taskList as $value){
                    $this->task($value);
            }
        }
    }

    //执行的任务

    public static function task($data){
        //添加定时器,返回定时器的ID
        $cron = new Cron();
        $timer_id = Timer::add(1,array($cron,'execute'),array($data),true);

        //更新任务定时器ID
        $task = new Task();
        $task->updateTimer($data,$timer_id);
        return $timer_id;
    }

    /**
     * 监听接收数据
     * @param $connection
     * @param $data
     */
    public function onMessage($connection, $data)
    {
        $data = trim($data);
        $data = json_decode($data,true);
        //任务ID
        $taskId = $data;
        //请求类型
        $type = $data;
        switch ($type){
            case 'add':
                //添加定时器
                $task = new Task();
                $list = $task->getTaskById($taskId);
                $timer_id = $this->task($list);
                //发送数据
                $data = array('code'=>1,'msg'=>'添加定时器成功');
                break;
            case 'stop':
                //删除定时器
                $timer_id = $data;//定时器ID
                //删除定时器
                $res = Timer::del($timer_id);
                if ($res){
                    //删除任务
                    $task = new Task();
                    $task->delTask($taskId);
                    $data = array('code'=>1, 'msg'=>'删除定时器成功');
                }else {
                    $data = array('code'=>0, 'msg'=>'删除定时器失败');
                }
                break;
            default:
                $data = array('code'=>505, 'msg'=>'非法请求');
                break;
        }

        //$data = array('code'=>1, 'msg'=>'删除定时器成功');
        return $connection->close(json_encode($data));

    }

    public static function onWorkerStop(){
        //清空定时任务
        $task  = new Task();
        $task->clearTimer();
    }

    public static function onError($connection, $code, $msg){
        return $msg;
    }
}

[attach]1275[/attach]

file:///C:\Users\zhang\AppData\Roaming\Tencent\Users\171051823\QQ\WinTemp\RichOle\3(%L9K6)8Z$H_ZUKW7WJ[7D.png

file:///C:\Users\zhang\AppData\Roaming\Tencent\Users\171051823\QQ\WinTemp\RichOle\3(%L9K6)8Z$H_ZUKW7WJ[7D.png
当浏览器调用onMessage时,并不会在新的进程运行,怎么样能在新的进程运行?

 

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