How to create threads in Perl?

前端 未结 3 1352
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 12:10

I have got easy Perl script where I have got a BIG loop and inside this I invoke more or less million times function my_fun(). I would like to create pool of th

3条回答
  •  暖寄归人
    2020-12-11 13:02

    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)

提交回复
热议问题