English 中文(简体)
我应研究在伯尔做多读的哪些单元?
原标题:What modules should I look at for doing multithreading in Perl?
  • 时间:2009-09-26 07:06:35
  •  标签:

我应研究在伯尔做多读的哪些单元?

我期望做一些相当低的表现;我想听起来的是同时管理多位工人,每个工人睡觉的时间各不相同。

最佳回答

最新版本的Perl得到了近似支持。 页: 1

$ perl -V:usethreads
usethreads= define 

>perldoc threads,为使用这些文本提供了很好的介绍。

问题回答

您可能不希望多面的原因有很多。 然而,如果你想要多面,那么以下法典可作为有用的范例。 它创造了一些工作,使那些工作处于read状态,然后开始一些把工作从胎盘上拉出并完成的工作。 每个校对都把工作从胎盘中拉开,直到再找不到工作。 该方案等待所有准备完成,然后印制了全部工作时间。

#!/usr/bin/perl

use threads;
use Thread::Queue;
use Modern::Perl;

my $queue= Thread::Queue->new;
my $thread_count= 4;
my $job_count= 10;
my $start_time= time;
my $max_job_time= 10;

# Come up with some jobs and put them in a thread-safe queue. Each job
# is a string with an id and a number of seconds to sleep. Jobs consist
# of sleeping for the specified number of seconds.
my @jobs= map {"$_," . (int(rand $max_job_time) + 1)} (1 .. $job_count);
$queue->enqueue(@jobs);

# List the jobs
say "Jobs IDs: ", join(", ", map {(split /,/, $_)[0]} @jobs);

# Start the threads
my @threads= map {threads->create(sub {function($_)})} (1 .. $thread_count);

# Wait for all the threads to complete their work
$_->join for (@threads);

# We re all done
say "All done! Total time: ", time - $start_time;

# Here s what each thread does. Each thread starts, then fetches jobs
# from the job queue until there are no more jobs in the queue. Then,
# the thread exists.
sub function {
  my $thread_id= shift;
  my ($job, $job_id, $seconds);
  while($job= $queue->dequeue_nb) {
    ($job_id, $seconds)= split /,/, $job;
    say "Thread $thread_id starting on job $job_id ",
      "(job will take $seconds seconds).";
    sleep $seconds;
    say "Thread $thread_id done with job $job_id.";
  }
  say "No more jobs for thread $thread_id; thread exiting.";
}

如果履约问题不成大问题,那么fork涉及多个过程,可能比处理校对更为容易。 我经常使用Parallel:ForkManager。 这非常简单,但非常好。

它像你一样,不需要先发制人多读;在这种情况下,看POE s cooperation model. 由于你的法典只有在你决定时才会产生其他的线索,而且你当时只剩下一线,所以发展和瓦解将变得更容易。

Coro是多个合作体的冰箱。

99%的时间是,如果你想要在Perl进行校对,这是你所需要的。

如果你愿意在有多个核心时加快你的代码,你就会走错的道路。 比其他语文低50x。 改写在两个荷兰邮联的法典意味着现在只有25x年比其他语文低......一个荷兰邮联。 更好地把缓慢部分带至不同语言。

但是,如果你不想让伊斯兰组织阻挡其他“read”,那么科罗就是你想要的。





相关问题
热门标签