私は大きなループを持っている簡単なPerlスクリプトを持っていますmy_fun()
. それを処理するスレッドのプールを作成したいと思います-同時に最大5つのスレッドがこのメソッドをループで呼び出します。
最速のライブラリを使用することは私にとって非常に重要です。例を見るのは本当に素晴らしいことです。
私のコードは次のようになります。
for (my $i = 0; $i < 1000000 ; $i++) {
my_fun();
}
前もって感謝します
私は大きなループを持っている簡単なPerlスクリプトを持っていますmy_fun()
. それを処理するスレッドのプールを作成したいと思います-同時に最大5つのスレッドがこのメソッドをループで呼び出します。
最速のライブラリを使用することは私にとって非常に重要です。例を見るのは本当に素晴らしいことです。
私のコードは次のようになります。
for (my $i = 0; $i < 1000000 ; $i++) {
my_fun();
}
前もって感謝します
Parallel::ForkManagerを見てください。スレッドではなくを使用してfork
いますが、仕事は非常に簡単に完了するはずです。
例はドキュメントから引用され、わずかに変更されています:
use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new(5); # number of parallel processes
for my $i (0 .. 999999) {
# Forks and returns the pid for the child:
my $pid = $pm->start and next;
#... do some work with $data in the child process ...
my_fun();
$pm->finish; # Terminates the child process
}
$pm->wait_all_children;
We can't give you the fastest way, since that depends on the work, and you didn't tell us what the work is.
But you did ask about threads, so I'll give you the foundation of a threaded application. Here is a worker model. It's robust, maintainable and extendable.
use threads;
use Thread::Queue qw( ); # Version 3.01+ required
my $NUM_WORKERS = 5;
sub worker {
my ($job) = @_;
...
}
my $q = Thread::Queue->new();
my @workers;
for (1..$NUM_WORKERS) {
push @workers, async {
while (defined(my $job = $q->dequeue())) {
worker($job);
}
};
}
$q->enqueue($_) for @jobs; # Send work
$q->end(); # Tell workers they're done.
$_->join() for @workers; # Wait for the workers to finish.
This is a basic flow (one-directional), but it's easy to make bi-directional by adding a response queue.
This uses actual threads, but you can switch to using processes by switching use threads;
to use forks;
.
Parallel::ForkManager can also be used to provide a worker model, but it's continually creating new processes instead of reusing them. This does allow it to handle child death easily, though.
Ref: Thread::Queue (or Thread::Queue::Any)
スレッドのドキュメントを参照してください。