在Perl中进行简单的并行处理 [英] simple parallel processing in perl

查看:409
本文介绍了在Perl中进行简单的并行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些对象的函数中,我有一些代码块可以并行运行并为我加快速度.

I have a few blocks of code, inside a function of some object, that can run in parallel and speed things up for me.

我尝试通过以下方式使用subs::parallel(所有这些都在函数的主体中):

I tried using subs::parallel in the following way (all of this is in a body of a function):

my $is_a_done = parallelize { 
                              # block a, do some work
                              return 1;
                             };
my $is_b_done = parallelize { 
                              # block b, do some work
                              return 1;
                             };
my $is_c_done = parallelize { 
                              # block c depends on a so let's wait (block)
                              if ($is_a_done) {
                               # do some work
                              };
                              return 1;
                             };
my $is_d_done = parallelize { 
                              # block d, do some work
                              return 1;
                             };

if ($is_a_done && $is_b_done && $is_c_done && $is_d_done) {
 # just wait for all to finish before the function returns
}

首先,请注意,我使用if等待线程阻塞,并在需要时等待上一个线程完成(更好的主意?if十分丑陋...).

First, notice I use if to wait for threads to block and wait for previous thread to finish when it's needed (a better idea? the if is quite ugly...).

第二,我得到一个错误:

Second, I get an error:

Thread already joined at /usr/local/share/perl/5.10.1/subs/parallel.pm line 259.
Perl exited with active threads:
    1 running and unjoined
    -1 finished and unjoined
    3 running and detached

推荐答案

我以前没有见过subs::parallel,但是考虑到它正在为您执行所有线程处理,而且似乎正在执行错误,根据错误消息,我认为这有点可疑.

I haven't seen subs::parallel before, but given that it's doing all of the thread handling for you, and it seems to be doing it wrong, based on the error message, I think it's a bit suspect.

通常我不会建议像那样扔掉它,但是使用纯线程接口,您所做的实际上并不困难,所以为什么不做个尝试,并且简化一下问题?同时,我会回答您问题的另一部分.

Normally I wouldn't just suggest throwing it out like that, but what you're doing really isn't any harder with the plain threads interface, so why not give that a shot, and simplify the problem a bit? At the same time, I'll give you an answer to the other part of your question.

use threads;
my @jobs;
push @jobs, threads->create(sub {
  # do some work
});

push @jobs, threads->create(sub {
  # do some other work
});

# Repeat as necessary :)

$_->join for @jobs; # Wait for everything to finish.

如果您使用这些子程序的返回值,则需要更复杂的东西(简单地切换到哈希值会很有帮助),但是在您提供的代码示例中,您忽略了它们,这使得事情很容易.

You need something a little bit more intricate if you're using the return values from those subs (simply switching to a hash would help a good deal) but in the code sample you provided, you're ignoring them, which makes things easy.

这篇关于在Perl中进行简单的并行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆