Swoole过程永远悬而未决

发布于 2025-01-27 05:43:30 字数 468 浏览 4 评论 0原文

我有最简单的吞咽代码,它可以睡一秒钟,并在屏幕上打印“运行任务”消息。

<?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";
    }
}

问题是它永远悬挂。但是,如果我删除睡眠(1),它将按预期运行并退出。那怎么了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2025-02-03 05:43:30

根据Swoole文档,您应该使用co :: Sleep()而不是sleep()

注意:本机php sleep() 如果没有 coroutine钩。但是,如果您已经启用了 coroutine hooks 推荐的方法只能使用,然后才能使用本地睡眠功能。结帐睡眠挂钩有关更多信息,您可以使用更多信息PHP功能在Coroutines内部。

swoole coroutine System:Sleep-sleep-sleep-正式文档

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文