Please introduce a multi-processing library in Perl or Ruby

后端 未结 5 750
囚心锁ツ
囚心锁ツ 2021-02-04 20:42

In python we can use multiprocessing modules. If there is a similar library in Perl and Ruby, would you teach it? I would appreciate it if you can include a brief sample.

5条回答
  •  猫巷女王i
    2021-02-04 20:49

    Have a look at this nice summary page for Perl parallel processing libs http://www.openfusion.net/perl/parallel_processing_perl_modules. I like Parallel::Forker, its a modern and more powerful library than the older Parallel::ForkManager and has more features like signalling child processes. I've used it in multiple projects and it works exactly as expected. Here's an example of how to use it:

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    use Parallel::Forker;
    
    my $forker = Parallel::Forker->new(use_sig_child => 1, max_proc => 4);
    $SIG{CHLD} = sub {
        Parallel::Forker::sig_child($forker);
    };
    $SIG{TERM} = sub {
        $forker->kill_tree_all('TERM') if $forker and $forker->in_parent;
        die "Exiting child process...\n";
    };
    # an example
    for (1..10) {
        $forker->schedule(run_on_start => sub {
            # all child process code to run here
        })->ready();
    }
    # wait for all child processes to finish
    $forker->wait_all();
    

    Hope this helps you

提交回复
热议问题