基于tp6 设置两个worker 怎么能同时启动

蛋挞树

问题描述

1.我是基于thinkphp6 然后在config目录里的worker_server.php文件 设置了worker_class属性为数组,两个worker服务,但是当我在命令行 启动worker:server 的时候 就只能启动数组第一个服务 第二个没有提示,是我的理解有问题还是业务上不能这么处理?
PS:我的业务逻辑是:创建不同的worker webscoket 端口 然后处理不同的业务,端口2345 处理 业务A,端口2346处理业务B。所以我设置了worker_class的数组属性

为此你搜索到了哪些方案及不适用的原因

然后通过chatGPT的提示说是第一个worker把第二个阻塞了?不太理解这个解释

业务A:文件workera.php

 public function __construct() {
        //实例化 WebSocket 服务
        $this->worker = new WorkerMan($this->socket);
        $type = Config::get('cache.default');
        $this->cachePrefix = Config::get('cache.stores')[$type]['prefix'];
        $this->workerManStartLock = $this->cachePrefix . Config::get('cache_option.prefix')['workerman_start_lock'];
        //系统类型
        $this->systemType = env('sign.system_type');

        $this->config = Config::get('rabbitmq')['channel'][$this->systemType];

        $this->setting = Setting::getItem("trade");
        //初始化
        if (!$this->init()) {
            return false;
        }

        //设置回调
        foreach (['onWorkerStart', 'onConnect', 'onMessage', 'onClose', 'onError', 'onBufferFull', 'onBufferDrain', 'onWorkerStop', 'onWorkerReload'] as $event) {
            if (method_exists($this, $event)) {
                $this->worker->$event = [$this, $event];
            }
        }
        //Run worker
        WorkerMan::runAll();
    }
    public function init(): bool
    {
        $this->processes = count($this->config);//3个
        $multiple = env('sign.multiple');
        $pidPath = env('runtime_path') .'/worker/.run/';
        $logPath = runtime_path() .'/worker/log/';
        !is_dir($logPath) && mkdir($logPath, 0755, true);
        !is_dir($pidPath) && mkdir($pidPath, 0755, true);

        $this->redis = Cache::store('redis')->handler();

        $this->worker->count    = $this->processes * $multiple; //18
        $this->worker->name     = 'recharge';

        WorkerMan::$daemonize   = true;
        WorkerMan::$logFile     = $logPath . 'workerman.log';
        WorkerMan::$stdoutFile  = $logPath . 'workerman_stdout.log';
        WorkerMan::$pidFile     = $pidPath . 'workerman.pid';
        return true;
    }

业务B:workerb.php

public function __construct()
    {
        $this->worker = new Worker($this->socket);
        $this->cliCmd = $_SERVER['argv'][1];

        //设置缓存前缀
        $type = Config::get('cache.default');
        $this->cachePrefix = Config::get('cache.stores')[$type]['prefix'];
        //设置控制多进程启动的锁对象
        $this->workerManStartLock = $this->cachePrefix . Config::get('cache_option.prefix')['workerman_start_lock'];

        //系统类型
        $this->systemType =env('jd_sign.machine');
        $this->config = Config::get('rabbitmq')['jd_channel'][$this->systemType];

        //初始化
        if (!$this->init()) {
            return false;
        }

        //设置回调
        foreach (['onWorkerStart', 'onConnect', 'onMessage', 'onClose', 'onError', 'onBufferFull', 'onBufferDrain', 'onWorkerStop', 'onWorkerReload'] as $event) {
            if (method_exists($this, $event)) {
                $this->worker->$event = [$this, $event];
            }
        }
        //Run worker
        Worker::runAll();
    }

    private function init(): bool
    {
        $this->processes    = count($this->config);//3进程:BIND_JD_BIG、CHECK_JD_TEMP、CHECK_JD
        $multiple           = env('jd_sign.multiple');
        //创建日志目录
        $pidPath            = env('jd_runtime_path.path') .'/worker/.run/';
        $logPath            = env('jd_runtime_path.path') . '/log/';
        !is_dir($logPath) && mkdir($logPath, 0755, true);
        !is_dir($pidPath) && mkdir($pidPath, 0755, true);

        $this->worker->count = $this->processes * $multiple; // 一个加急进程 multiple = 7  count = 3*7

        $this->worker->name  = 'jdrecharge';
        Worker::$daemonize   = true;
        Worker::$logFile     = $logPath . 'workerman.log';
        Worker::$stdoutFile  = $logPath . 'workerman_stdout.log';
        Worker::$pidFile     = $pidPath . 'workerman.pid';

        return true;
    }
1345 0 0
0个回答

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