swoole-inc / report

关于 Swoole 文档中的内容问题(如错别字、示例错误、内容缺失等)以及需求建议
https://wiki.swoole.com
7 stars 2 forks source link

[Doc] Channel push $timeout参数 #38

Closed yuntian001 closed 3 years ago

yuntian001 commented 3 years ago
sy-records commented 3 years ago
  1. push设置了超时时间,没有pop,到达时间后就会发生超时,底层会恢复当前协程,push 调用会返回 false,并设置错误码为-1 (SWOOLE_CHANNEL_TIMEOUT)
  2. push 设置了 -1,没有pop,那么协程挂起,这个协程确实是有问题的。
yuntian001 commented 3 years ago
  1. push设置了超时时间,没有pop,到达时间后就会发生超时,底层会恢复当前协程,push 调用会返回 false,并设置错误码为-1 (SWOOLE_CHANNEL_TIMEOUT)
  2. push 设置了 -1,没有pop,那么协程挂起,这个协程确实是有问题的。

push 设置了 -1或者0 没有pop 现在抛异常这个是属于bug,还是就这样设计的。

sy-records commented 3 years ago

4.6新加的功能,协程死锁检测,可以手动关闭。 https://wiki.swoole.com/#/coroutine/coroutine?id=set

use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use function Swoole\Coroutine\run;

Coroutine::set(['enable_deadlock_check' => false]);
run(function(){
    $channel = new Channel(1);
    Coroutine::create(function () use ($channel) {
        $timeOut = 0;
        $channel->push(['rand' => rand(1000, 9999),'number'=>1]);
        echo "协程1第一次入通道\n";
        echo "协程1检测通道状态:is full\n";
        var_dump($channel->isFull());
        echo "协程1第二次入通道:\n";
        $res = $channel->push(['rand' => rand(1000, 9999),'number'=>2], $timeOut);
        echo "协程1第二次入通道结果:\n";
        var_dump($res);
        echo "协程1第三次入通道:\n";
        $res = $channel->push(['rand' => rand(1000, 9999),'number'=>3], $timeOut);
        echo "协程1第三次入通道结果:\n";
        var_dump($res);
    });
    Coroutine::create(function () use ($channel) {
        echo "协程2\n";
        \Swoole\Coroutine\System::sleep(5);
        echo "协程2 5秒后\n";
        $data = $channel->pop();
        echo "协程2第一次挂起后收到数据:\n";
        var_dump($data);
        \Swoole\Coroutine\System::sleep(5);
        echo "协程2 10秒后\n";
    });
    \Swoole\Coroutine::create(function (){
        echo "协程3\n";
    });
});
yuntian001 commented 3 years ago
Coroutine::set(['enable_deadlock_check' => false]);

多谢,已了解。