不知何故,我无法得到File :: Queue的想法。对于非阻塞并行执行,我最终使用了TheSchwartz和Parallel :: Prefork的组合,就像它在Foorum Catalyst App中执行的那样。基本上,有5个重要元素。也许这个总结会对别人有帮助。
1)TheSchwartz DB
2)客户端(DB句柄)的TheSchwartz DB
package MyApp::TheSchwartz::Client;
use TheSchwartz;
sub theschwartz {
my $theschwartz = TheSchwartz->new(
databases => [ {
dsn => 'dbi:mysql:theschwartz',
user => 'user',
pass => 'pass',
} ],
verbose => 1,
);
return $theschwartz;
}
3)作业工人(其中实际工作已经完成)
package MyApp::TheSchwartz::Worker::Test;
use base qw(TheSchwartz::Moosified::Worker);
use MyApp::Model::DB; # Catalyst DB connect_info
use MyApp::Schema; # Catalyst DB schema
sub work {
my $class = shift;
my $job = shift;
my ($args) = $job->arg;
my ($arg1, $arg2) = @$args;
# re-use Catalyst DB schema
my $connect_info = MyApp::Model::DB->config->{connect_info};
my $schema = MyApp::Schema->connect($connect_info);
# do the heavy lifting
$job->completed();
}
4)工作进程TheSchwartzWorker.pl
,其监视表工作不停止
use MyApp::TheSchwartz::Client qw/theschwartz/; # db connection
use MyApp::TheSchwartz::Worker::Test;
use Parallel::Prefork;
my $client = theschwartz();
my $pm = Parallel::Prefork->new({
max_workers => 16,
trap_signals => {
TERM => 'TERM',
HUP => 'TERM',
USR1 => undef,
}
});
while ($pm->signal_received ne 'TERM') {
$pm->start and next;
$client->can_do('MyApp::TheSchwartz::Worker::Test');
my $delay = 10; # When no job is available, the working process will sleep for $delay seconds
$client->work($delay);
$pm->finish;
}
$pm->wait_all_children();
5)在催化剂控制:插入一个新的工作到表工作和传递一些参数
use MyApp::TheSchwartz::Client qw/theschwartz/;
sub start : Chained('base') PathPart('start') Args(0) {
my ($self, $c) = @_;
$client = theschwartz();
$client->insert(‘MyApp::TheSchwartz::Worker::Test’, [ $arg1, $arg2 ]);
$c->response->redirect(
$c->uri_for(
$self->action_for('archive'),
{mid => $c->set_status_msg("Run '$name' started")}
)
);
}
新的运行是灰色的“档案”页面上,直到所有的结果都可以在数据库中。
做上传作为异步请求是有道理的。返回一个作业ID,并在Action完成后在Action中设置一个标志。然后让你的页面定期异步地轮询后端(比如每10秒),如果它获得_done_,则刷新页面。我会稍微输入一个答案。 – simbabque
关于您的主机托管问题,它实际上取决于用例。亚马逊或其他云服务的优势在于,如果需要,可以轻松扩展,但除了其他考虑事项外,可能比拥有自己的服务器更昂贵。这个问题应该最好单独和其他地方发布。 – bytepusher
@simbabque如果您有时间,有用的工具/插件提示或一些示例代码会非常有帮助。 感谢bytepusher,我会考虑应用程序投入生产时 –