How do I sleep for shorter than a second in Perl?
我怎么在Perl中睡不到一秒钟?
6 个解决方案
#1
99
From the Perldoc page on sleep:
从睡眠中的Perldoc页面:
For delays of finer granularity than one second, the Time::HiRes module (from CPAN, and starting from Perl 5.8 part of the standard distribution) provides usleep().
对于比一秒钟更精细的延迟,Time :: HiRes模块(来自CPAN,从标准分布的Perl 5.8部分开始)提供了usleep()。
Actually, it provides usleep()
(which sleeps in microseconds) and nanosleep()
(which sleeps in nanoseconds). You may want usleep()
, which should let you deal with easier numbers. 1 millisecond sleep (using each):
实际上,它提供了usleep()(以微秒为单位)和nanosleep()(以纳秒为单位)。你可能想要usleep(),它可以让你处理更容易的数字。 1毫秒睡眠(使用每个):
use strict;
use warnings;
use Time::HiRes qw(usleep nanosleep);
# 1 millisecond == 1000 microseconds
usleep(1000);
# 1 microsecond == 1000 nanoseconds
nanosleep(1000000);
If you don't want to (or can't) load a module to do this, you may also be able to use the built-in select()
function:
如果您不想(或不能)加载模块来执行此操作,您也可以使用内置的select()函数:
# Sleep for 250 milliseconds
select(undef, undef, undef, 0.25);
#2
34
Time::HiRes:
时间::高分辨率:
use Time::HiRes;
Time::HiRes::sleep(0.1); #.1 seconds
Time::HiRes::usleep(1); # 1 microsecond.
http://perldoc.perl.org/Time/HiRes.html
http://perldoc.perl.org/Time/HiRes.html
#3
12
From perlfaq8:
来自perlfaq8:
How can I sleep() or alarm() for under a second?
如何在一秒钟内睡觉()或闹钟()?
If you want finer granularity than the 1 second that the sleep() function provides, the easiest way is to use the select() function as documented in select in perlfunc. Try the Time::HiRes and the BSD::Itimer modules (available from CPAN, and starting from Perl 5.8 Time::HiRes is part of the standard distribution).
如果你想要比sleep()函数提供的1秒更精细的粒度,最简单的方法是使用select in perlfunc中记录的select()函数。尝试Time :: HiRes和BSD :: Itimer模块(可从CPAN获得,从Perl 5.8开始Time :: HiRes是标准发行版的一部分)。
#5
4
A quick googling on "perl high resolution timers" gave a reference to Time::HiRes. Maybe that it what you want.
关于“perl高分辨率计时器”的快速Google搜索提供了对Time :: HiRes的引用。也许这就是你想要的。
#6
-3
system "sleep 0.1";
does the trick.
诀窍。
#1
99
From the Perldoc page on sleep:
从睡眠中的Perldoc页面:
For delays of finer granularity than one second, the Time::HiRes module (from CPAN, and starting from Perl 5.8 part of the standard distribution) provides usleep().
对于比一秒钟更精细的延迟,Time :: HiRes模块(来自CPAN,从标准分布的Perl 5.8部分开始)提供了usleep()。
Actually, it provides usleep()
(which sleeps in microseconds) and nanosleep()
(which sleeps in nanoseconds). You may want usleep()
, which should let you deal with easier numbers. 1 millisecond sleep (using each):
实际上,它提供了usleep()(以微秒为单位)和nanosleep()(以纳秒为单位)。你可能想要usleep(),它可以让你处理更容易的数字。 1毫秒睡眠(使用每个):
use strict;
use warnings;
use Time::HiRes qw(usleep nanosleep);
# 1 millisecond == 1000 microseconds
usleep(1000);
# 1 microsecond == 1000 nanoseconds
nanosleep(1000000);
If you don't want to (or can't) load a module to do this, you may also be able to use the built-in select()
function:
如果您不想(或不能)加载模块来执行此操作,您也可以使用内置的select()函数:
# Sleep for 250 milliseconds
select(undef, undef, undef, 0.25);
#2
34
Time::HiRes:
时间::高分辨率:
use Time::HiRes;
Time::HiRes::sleep(0.1); #.1 seconds
Time::HiRes::usleep(1); # 1 microsecond.
http://perldoc.perl.org/Time/HiRes.html
http://perldoc.perl.org/Time/HiRes.html
#3
12
From perlfaq8:
来自perlfaq8:
How can I sleep() or alarm() for under a second?
如何在一秒钟内睡觉()或闹钟()?
If you want finer granularity than the 1 second that the sleep() function provides, the easiest way is to use the select() function as documented in select in perlfunc. Try the Time::HiRes and the BSD::Itimer modules (available from CPAN, and starting from Perl 5.8 Time::HiRes is part of the standard distribution).
如果你想要比sleep()函数提供的1秒更精细的粒度,最简单的方法是使用select in perlfunc中记录的select()函数。尝试Time :: HiRes和BSD :: Itimer模块(可从CPAN获得,从Perl 5.8开始Time :: HiRes是标准发行版的一部分)。
#4
#5
4
A quick googling on "perl high resolution timers" gave a reference to Time::HiRes. Maybe that it what you want.
关于“perl高分辨率计时器”的快速Google搜索提供了对Time :: HiRes的引用。也许这就是你想要的。
#6
-3
system "sleep 0.1";
does the trick.
诀窍。