温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

perl使用Mojolicious设置threads查看设置参数

发布时间:2020-06-27 10:53:03 来源:网络 阅读:1053 作者:RZL_01 栏目:开发技术


很无聊。。。随便写D代码 不得不说perl的多线程简单易懂

加个ajax就好了


use Mojolicious::Lite;
use threads;
use threads::shared;
#请直接使用 ./x.pl daemon 启动
#morbo启动,线程不会自动关闭。要手动关闭改代码才行,不然创建的线程仍会执行
#当然你的线程不是个死循环,使用morbo也是不错的。线程执行完就会自动退出
#当然你可以使用其他方式避免线程句柄被重置。

#线程共享变量,
my $signal:shared=-1;
my $i:shared=0;
#线程句柄
my $thr;

#接受POST GET都是它。
any '/status' => sub {
  my $c = shift;
#只要模块里有要使用的,即使是空值都要传过去,不然在模块里会报错。
#Global symbol "$xxx" requires explicit package name
  $c->stash(status => $signal);
  $c->stash(i => $i);
  $c->stash(thr => $thr);
#数据限制。状态设置,判断参数使用字符串判断是否为空
  if ($c->param('signal') ne '' && $c->param('signal') >=-1 && $c->param('signal')<=1) {
  $signal=$c->param('signal');
  $c->stash(status => $signal);
  undef $thr if $signal == -1 ;
  $c->stash(thr => $thr);
  }
#根据传进来的参数创建线程
  if ($c->param('action')) {
    my $action=$c->param('action');
    if ($action eq 'create') {
      $thr = threads->new(\&ppp,1) unless $thr ;
      $c->stash(thr => $thr);
    }
  }
  $c->render('status');
};

#线程函数
sub ppp {
#接受第一个参数
    $signal=shift;
#0的时候跟false一样
    while($signal or $signal==0){
#睡一秒钟
        sleep 1;
        last if $signal == -1;
#再睡一秒钟
        sleep 1 if $signal == 0;
#print要换行符才会有输出,不然要等到mojo结束了才有输出。
        print "running.\t.".$i++."\n" if ($signal == 1 ) 

    }
}
app->start;

__DATA__
@@ status.html.ep
% unless ($thr) {
no threads creat or threads down bbbba<br>
%} else {
<meta http-equiv="refresh" content="5">
%}
The status Can be exit=-1  stop=0 start=1 <br>
The status now is  <%= $status %> .i=<%= $i %> .thr=<%= $thr if defined $thr %> 
%= form_for '/status' => (method => 'POST') => begin
  %= text_field 'signal'
  %= submit_button '设置状态'
% end
%= form_for '/status' => (method => 'POST') => begin
  %= hidden_field action => 'create'
  %= submit_button '创建线程'
% end

perl使用Mojolicious设置threads查看设置参数

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI