'Swoole\Process pop in blocking and non-blocking mode

Documentation says, that Swoole\Process::pop can run in blocking and non-blocking mode:

If failed, it returns FALSE. Or it returns the data. In the blocking mode, if the queue is empty, the call of this method will block. In the non-blocking mode, if the queue is empty, the call of this method will return FALSE immediately and set the error code to ENOMSG.

But how can I run it in blocking and non-blocking modes? Official documentation does not provide any clues. I tried some simplest code and it seems like pop is blocking. This is some minimal reproducible code:

    $process1 = new \Swoole\Process(function ($process) {
        while (true) {
            sleep(5);
            $ret = $process->pop();
            if ($ret !== false) {
                echo "exit process 1\n";
                break;
            }
        }
        echo "exit process 1 while loop\n";
    }, false);
    $process1->useQueue(10, 2);

    $process2 = new \Swoole\Process(function ($process) {
        $i = 0;
        while (true) {
            sleep(5);
            $ret = $process->pop();
            if ($ret !== false) {
                echo "exit process 2\n";
                break;
            }
            $i += 1;
            if ($i >= 3) {
                break;
            }
        }
        echo "exit process 2 while loop\n";
    }, false);
    $process2->useQueue(10, 1);

    $process1->start();
    $process2->start();
    sleep(1);
    $process1->push("1");
    \Swoole\Process::wait();

If you run it, you will get this on your screen:

exit process 1
exit process 1 while loop

How can I make the second process pop, wait, exit its loop and not hang forever? Now it seems like pop is blocking. But I want it to work in a non-blocking mode.



Sources

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

Source: Stack Overflow

Solution Source