perl Coro共享阵列

时间:2015-04-06 20:36:26

标签: perl coroutine

我在编程方面有点新意,现在对于一个简单的任务有点困惑(很好) - 在coro协程之间共享一个数组。

像这样(不工作,只需要40秒)...

my @array = (1..1000);
$|=0;

sub start_thread($) {
    my $url = shift;
    return async {
        print "starting  $array[0]\n";
        shift @array;
        sleep(2);
      };
}

sub main{
  start_thread $_ for (1..20);
  EV::loop;
}

main;

我已经阅读过IPC :: Sharelite和IPC :: Share并尝试了一些东西,但没有任何工作...... 如果有人能清楚地指出我吗?

1 个答案:

答案 0 :(得分:4)

Coro是一个合作多任务系统。这意味着一次只能运行一个任务,并且您可以通过使用事件系统(例如$cv->recv)而不是阻止调用(例如sleep)来让其他任务运行。

use AE   qw( );
use Coro qw( async );

sub ae_sleep {
   my ($secs) = @_;
   my $cv = AE::cv();
   my $guard = AE::timer($secs, 0, $cv);
   $cv->recv();
}

sub worker {
   my ($job) = @_;
   my $id = sprintf("Thread %d: Job %s", $Coro::current, $job);
   print("$id: Running...\n");
   ae_sleep(2);
   print("$id: done.\n");
}

{
   my @array = 1..60;

   my @threads;
   for (1..20) {
      push @threads, async {
         while (defined(my $job = shift(@array))) {
            worker($job);
         }
      };
   }

   $_->join() for @threads;
}

如果使用Coro :: Channel而不是数组,则可以在启动工作线程后向队列中添加元素。

use AE            qw( );
use Coro          qw( async );
use Coro::Channel qw( );

sub ae_sleep { ... }  # Same as above

sub worker { ... }  # Same as above

{
   my $q = Coro::Channel->new();

   my @threads;
   for (1..20) {
      push @threads, async {
         while (defined(my $job = $q->get())) {
            worker($job);
         }
      };
   }

   $q->put($_) for 1..60;

   $q->shutdown();
   $_->join() for @threads;
}

如果你想使用真正的线程,那就简单地说:

use threads;
use Thread::Queue qw( );  # 3.01+

sub worker {
   my ($job) = @_;
   my $id = sprintf("Thread %d: Job %s", threads->tid, $job);
   print("$id: Running...\n");
   sleep(2);
   print("$id: done.\n");
}

{
   my $q = Thread::Queue->new();

   my @threads;
   for (1..20) {
      push @threads, async {
         while (defined(my $job = $q->dequeue())) {
            worker($job);
         }
      };
   }

   $q->enqueue($_) for 1..60;

   $q->end();
   $_->join() for @threads;
}