第一個Work mqWorker 里非得執(zhí)行 Swoole\Runtime::enableCoroutine(true);
不然onWorkerStart 就會阻塞執(zhí)行, 嘗試了很多種寫法都不行
在文件 開始 調(diào)用 enableCoroutine, 又會報錯
API must be called in coroutine
跟蹤了代碼,發(fā)現(xiàn)是調(diào)用unixsocket的函數(shù)報錯了, 感覺是調(diào)用了enableCorotine后, 阻塞函數(shù)都不能用了似的
目前我的示例代碼可以按照我的意愿執(zhí)行的, 但是比較惡心的是需要單獨調(diào)用enableCoroutine, 不能有滿意的解釋, 這個代碼不敢用到生產(chǎn)環(huán)境中.
mq_loop_recv 函數(shù)是我調(diào)用PHP rabbitmq 的循環(huán)收消息函數(shù)
$mqWoker = new Worker();
//啟動2個進程對外服務(wù)
$mqWoker->count = 1;
$mqWoker->onWorkerStart = function($mqWoker)
{
printf("will Exec mq_loop_recv\n");
Swoole\Runtime::enableCoroutine(true);
go("mq_loop_recv");
printf("will Exec mq_loop_recv over\n");
};
$threadWorker1 = new Worker();
$threadWorker1->onWorkerStart = function ($threadWorker1){
go(function () {
sleep(5);
printf( "threadWorker1 is alive!\n" );
});
};
$threadWorker2 = new Worker();
$threadWorker2->onWorkerStart = function ($threadWorker2){
go(function () {
sleep(5);
printf( "threadWorker2 is alive!\n" );
});
};
Worker::$eventLoopClass = 'Workerman\Events\Swoole';
//go(function () {
// Worker::runAll();
//});
Worker::runAll();