gpt4 book ai didi

perl - 在 while 循环中 sleep 导致无响应

转载 作者:行者123 更新时间:2023-12-04 23:21:54 25 4
gpt4 key购买 nike

我写了一个 sleepwhile环形。这是一个非常简单的代码,但没有用。

use strict;
use warnings;

&main();
sub main()
{
print "hello\n";

while(1) # #1
{
print "go~ ";
sleep 2;
}
}

如果注释掉#1,则打印“go~”;否则,它只是等待没有任何“go~”打印。
我的意图是定期做一些事情。
谁能给出一些解释/提示?

最佳答案

尝试在 go 后添加新行~

    use strict;
use warnings;

&main();
sub main()
{
print "hello\n";

while(1) # #1
{
print "go~\n";
sleep(2);
}
}

解释它为什么起作用:
stdout 流是缓冲的,因此只会在到达换行符后(或当它被告知时)才显示缓冲区中的内容。您没有使用换行符,因此文本将被附加到缓冲区,直到缓冲区的大小。

如果您不想使用换行符,请在开头添加以下行
use IO::Handle;
STDOUT->autoflush(1);

关于perl - 在 while 循环中 sleep 导致无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23283319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com