作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个基本的Perl脚本,该脚本运行具有20个线程的函数。
use threads;
use threads::shared;
use Thread::Queue;
use File::Slurp;
$| = 1; my $numthreads = 20;
my $fetch_q = Thread::Queue->new();
sub fetch {
while ( my $target = $fetch_q->dequeue() ) {
my $request = `curl "http://WEBSITE" -s -o /dev/null -w "%{http_code}"`; # Returns HTTP Status code of request (i.e. 200, 302, 404, etc)
if ($request eq "200") {
print "Success. Sleeping for 5 seconds.";
sleep(5);
}
else {
print "Fail. Will try again in 10 seconds.";
sleep(10);
redo;
}
}
}
my @workers = map { threads->create( \&fetch ) } 1 .. $numthreads;
$fetch_q->enqueue( 1 .. $max );
$fetch_q->end();
foreach my $thr (@workers) {$thr->join();}
sleep(5)
时,整个脚本似乎休眠了5秒钟。
sleep()
?
最佳答案
sleep
是正确的工具。我不知道您为什么认为它似乎阻塞了所有线程,但事实并非如此。下面说明了这一点:
use threads;
use threads::shared;
use Thread::Queue;
my $fetch_q = Thread::Queue->new();
sub fetch {
while (defined( my $job = $fetch_q->dequeue() )) {
if ($job) {
print threads->tid, " Starting to sleep\n";
sleep(5);
print threads->tid, " Finished sleeping\n";
}
else {
print threads->tid, " Starting to sleep\n";
sleep(10);
print threads->tid, " Finished sleeping\n";
}
}
}
my @workers = map { threads->create( \&fetch ) } 1 .. 2;
$fetch_q->enqueue($_) for 0, 1, 1;
$fetch_q->end();
foreach my $thr (@workers) {$thr->join();}
1 Starting to sleep
2 Starting to sleep
2 Finished sleeping
2 Starting to sleep <-- The second job picked up work while the first was still sleeping.
1 Finished sleeping
2 Finished sleeping
关于multithreading - Perl:让线程进入休眠状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32260595/
我有一个 powershell 进程,它从远程服务器读取记录并将它们复制到本地数据库中。运行时,它可能会运行 8-12 小时。 如何防止计算机在此期间关闭(或进入休眠/休眠模式)?我知道我可以调整“电
是否有一些命令行或 AppleScript 可以编写/运行以使机器自动进入休眠状态,甚至更好地进入休眠模式? 我可以以 root 身份运行该程序。 最佳答案 osascript -e 'tell ap
我是一名优秀的程序员,十分优秀!