'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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samuel Hassid