Mojolicious 中用于处理不同路径的全局变量和线程

发布于 2025-01-07 23:22:55 字数 1392 浏览 0 评论 0原文

在我的 Mojolicious perl 代码中,我处理从远程客户端创建和监视的作业。

我将作业保存在哈希数组中,这是一个全局变量。

然后它在 PUT '/job/create' 和 GET '/job/status' 的处理程序中使用。 当使用 PUT '/job/create' 添加新作业时,数组得到 在子例程中扩展(它包含下面代码中的 4 个元素), 但是当通过 GET '/job/status' 请求作业状态时,列表 jobs,数组不包含添加的元素(计数为 2 元素)。

谢谢,Jan

这是代码:

#!/usr/bin/perl -w

use threads; 
use threads::shared; 
use Mojolicious::Lite; 
use Mojo::JSON; 
my (%record, %job1, %job2, %job3, @jobs) : shared; 

%job1 = ( id=>"id1"); 
%job2 = ( id=>"id2"); 
%job3 = ( id=>"id3"); 

push ( @jobs, \%job1 ); 
push ( @jobs, \%job2 ); 

app->config(hypnotoad => {listen => ['http://*:3000']}); 

put '/job/create' => sub { 
    my $self = shift; 
    my $obj = Mojo::JSON->decode( $self->req->body ); 
    my $id = $obj->{id}; 
    %record = (id => $id); 
    push ( @jobs, \%record ); # test the global prefilled 
    push ( @jobs, \%job3 );   # test the global locally filled 
    $self->render(text => "Created job id $id. Jobs count: " . 
$#jobs ); 
}; 

get '/job/status' => sub { 
    my $self = shift; 
    my $out = "["; 
    for(my $i=0; $i<$#jobs+1; $i++) { 
        $out .= "{id:\""  . $jobs[$i]{id}      . "\","; 
        $out .= "," if $i<$#jobs; 
    } 
    $out .= "]"; 
    $self->render(text => "allJobsInfo($out). Num jobs: " . $#jobs); 
};

app->start();

In my Mojolicious perl code I handle a jobs created and watched from a remote client.

I keep the jobs in a array of hashes, which is a global variable.

It is then used in handlers of PUT '/job/create' and GET '/job/status'.
When adding a new job with the PUT '/job/create' the array gets
extended in the subroutine (it contains 4 elements in the code below),
but when requesting the jobs' status via GET '/job/status' the list of
jobs, the array does not contain the added elements (it counts 2
elements).

Thanks, Jan

Here is the code:

#!/usr/bin/perl -w

use threads; 
use threads::shared; 
use Mojolicious::Lite; 
use Mojo::JSON; 
my (%record, %job1, %job2, %job3, @jobs) : shared; 

%job1 = ( id=>"id1"); 
%job2 = ( id=>"id2"); 
%job3 = ( id=>"id3"); 

push ( @jobs, \%job1 ); 
push ( @jobs, \%job2 ); 

app->config(hypnotoad => {listen => ['http://*:3000']}); 

put '/job/create' => sub { 
    my $self = shift; 
    my $obj = Mojo::JSON->decode( $self->req->body ); 
    my $id = $obj->{id}; 
    %record = (id => $id); 
    push ( @jobs, \%record ); # test the global prefilled 
    push ( @jobs, \%job3 );   # test the global locally filled 
    $self->render(text => "Created job id $id. Jobs count: " . 
$#jobs ); 
}; 

get '/job/status' => sub { 
    my $self = shift; 
    my $out = "["; 
    for(my $i=0; $i<$#jobs+1; $i++) { 
        $out .= "{id:\""  . $jobs[$i]{id}      . "\","; 
        $out .= "," if $i<$#jobs; 
    } 
    $out .= "]"; 
    $self->render(text => "allJobsInfo($out). Num jobs: " . $#jobs); 
};

app->start();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2025-01-14 23:22:55

这实际上行不通,因为 hypnotoad 使用的是 fork,而不是线程。我建议将数据存储在数据库或 Cache::FastMmap 中。

This won't really work, as hypnotoad is using fork, not threads. I suggest storing data in something like a database or Cache::FastMmap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文