simple parallel processing in perl

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

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

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 } 

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 

回答1:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!