简体   繁体   中英

Swoole process is hanging forever

I have the simplest Swoole code, which sleeps for a second and prints "Run task" message to the screen.

<?php

namespace Tests\Util;

use PHPUnit\Framework\TestCase;

class MultiprocessingTest extends TestCase
{
    public function testProcess(): void
    {
        $t = new \Swoole\Process(function ($process) {
            sleep(1);
            echo "Run task\n";
        }, false);
        $t->start();
        echo "Start main process!\n";
    }
}

The problem is that it hangs forever. But if I remove sleep(1), it runs and exits as expected. What is wrong with that?

According the Swoole documentation, you should use co::sleep() instead of sleep()

Note: The native PHP sleep() function should not be used within a coroutine context without coroutine hooks . However, if you have enabled coroutine hooks the recommended approach is to then only use the native sleep function. Checkout the sleep hook for more information, this allows you to use native PHP functions inside coroutines.

Swoole Coroutine System: sleep - Official documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM