0

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?

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community May 09 '22 at 19:35

1 Answers1

0

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

Samuel Hassid
  • 627
  • 5
  • 10