我正在Internet上搜索有关此信息
Threads::Pool
,Threads::Queue
...但是我不能确定哪一种对我的情况更好。有人可以给我一些建议吗?
最佳答案
您可以使用Thread::Queue和线程。
进程之间的IPC(线程之间的通信)更加容易。
To fork or not to fork?
use strict;
use warnings;
use threads;
use Thread::Queue;
my $q = Thread::Queue->new(); # A new empty queue
# Worker thread
my @thrs = threads->create(sub {
while (my $item = $q->dequeue()) {
# Do work on $item
}
})->detach() for 1..10;#for 10 threads
my $dbh = ...
while (1){
#get items from db
my @items = get_items_from_db($dbh);
# Send work to the thread
$q->enqueue(@items);
print "Pending items: "$q->pending()."\n";
sleep 15;#check DB in every 15 secs
}
关于multithreading - Perl线程模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21567631/