国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

GatewayWorker的onWorkerStart方法中如何獲得單進程的id

ximengxuan

在GatewayWorker的onWorkerStart方法中,調(diào)用了Crontab模塊。
gateway進程數(shù)設置為4,經(jīng)過測試,發(fā)現(xiàn)每次執(zhí)行都會出現(xiàn)4個結(jié)果。
有什么辦法能讓Crontab模塊只執(zhí)行1次,而不是執(zhí)行多次呢?

使用$pid = posix_getpid()方法,獲取到的是5位數(shù)的id,數(shù)字是隨機的,并不固定。
有什么辦法能獲得$pid映射的$worker_id,結(jié)果如0、1、2、3這樣的呢?

//GatewayWorker/Applications/YourApp/Events.php

use Workerman\Crontab\Crontab;

public static function onWorkerStart($businessWorker)
    {
        // 每分鐘的第1秒執(zhí)行.
        self::$crontab = new Crontab('1 * * * * *', function () {
            // echo date('Y-m-d H:i:s') . "\n";

            echo posix_getpid() . "\n";
        });
    }

輸出內(nèi)容:

13080
13091
13093
13094

還有一個問題,就是打印$businessWorker這個參數(shù),輸出的是null。這個是什么原因呢?

1018 3 0
3個回答

ximengxuan

測試了一下,設置$worker->count = 1;
就可以實現(xiàn)Crontab模塊只執(zhí)行1次。
不知道還有沒有其他更好的辦法?

  • 暫無評論
ximengxuan

通過查詢開發(fā)手冊和論壇搜索,目前已找到另外兩種辦法。
1、在start_gateway.php所在目錄里建立一個start_worker.php,內(nèi)容類似

<?php

use Workerman\Worker;
use Workerman\Timer;

require_once __DIR__ . '/../../vendor/autoload.php';

$worker = new Worker("http://0.0.0.0:1258");

// 回調(diào)函數(shù)
$worker->onWorkerStart = function ($worker) {
    Timer::add(3, function () {
        echo posix_getpid() . "——" . date('Y-m-d H:i:s') . "\n";
    });
};

if (!defined('GLOBAL_START')) {
    Worker::runAll();
}

以上內(nèi)容來自http://wtbis.cn/q/8853

2、添加一個回調(diào)函數(shù):

//GatewayWorker/Applications/YourApp/start_businessworker.php

use \Workerman\Timer;

$worker->onWorkerStart = function ($worker) {
    // 只在id編號為0的進程上設置定時器,其它1、2、3號進程不設置定時器
    if ($worker->id === 0) {
        Timer::add(3, function () {
            echo "4個worker進程,只在0號進程設置定時器\n";
        });
    } else {
        echo "worker->id={$worker->id}\n";
    }
};
  • 暫無評論
walkor 打賞

也可以這樣Event.php里

calss Event
{
   public static function onWorkerStart($worker)
   {
       if($worker->id = 0) {

       }
   }
}
  • ximengxuan 2023-06-11

    這樣會報錯的。
    Deprecated: call_user_func() expects parameter 1 to be a valid callback, non-static method Events::onWorkerStart() should not be called statically in /www/wwwroot/api.xxx.cn/GatewayWorker/vendor/workerman/gateway-worker/src/BusinessWorker.php on line 214

  • walkor 2023-06-11

    public static function onWorkerStart($worker)

  • ximengxuan 2023-06-11

    對啊,我是這樣實現(xiàn)的。文件路徑是“GatewayWorker/Applications/YourApp/Events.php”,在Events類下,public static function onWorkerStart($businessWorker),無法打印$businessWorker

  • walkor 2023-06-11

    檢查下加沒加 static

  • ximengxuan 2023-06-11

    直接復制您上面的代碼,放在Events類里。啟動服務后,會拼命地打印“ Gateway: Worker->name conflict. Key:127.0.0.1:YourAppBusinessWorker:0”

  • walkor 2023-06-11

    如果啟動多組BusinessWorker,BusinessWorker->name不能一樣

年代過于久遠,無法發(fā)表回答
??