WorkerMan 和Swoole 一起用碰到的疑问

debugman

第一个Work mqWorker 里非得执行 Swoole\Runtime::enableCoroutine(true);
不然onWorkerStart 就会阻塞执行, 尝试了很多种写法都不行
在文件 开始 调用 enableCoroutine, 又会报错
API must be called in coroutine
跟踪了代码,发现是调用unixsocket的函数报错了, 感觉是调用了enableCorotine后, 阻塞函数都不能用了似的

目前我的示例代码可以按照我的意愿执行的, 但是比较恶心的是需要单独调用enableCoroutine, 不能有满意的解释, 这个代码不敢用到生产环境中.

mq_loop_recv 函数是我调用PHP rabbitmq 的循环收消息函数

$mqWoker = new Worker();

//启动2个进程对外服务
$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();
2704 1 0
1个回答

debugman

我是希望用worman 来写常驻服务, 里面的协程可以实现主动调度, 不用来消息触发

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