加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何从另一个进程调用正在运行的Perl进程中的方法?

发布时间:2020-12-15 23:28:14 所属栏目:大数据 来源:网络整理
导读:好的,所以,我得到了信号通过但由于某种原因,该过程在收到信号后存在. 如果我在创建套接字之前添加无限循环(while(1)),那么它按规定工作. 所以……当我发出kill命令时,套接字代码中的某些东西就会退出. 我不知道它会是什么.如果没有kill,进程就会无限期地接受
好的,所以,我得到了信号通过但由于某种原因,该过程在收到信号后存在.

如果我在创建套接字之前添加无限循环(while(1)),那么它按规定工作.
所以……当我发出kill命令时,套接字代码中的某些东西就会退出.

我不知道它会是什么.如果没有kill,进程就会无限期地接受连接并向客户端发送消息.为什么kill(以及后面的变量的增量)会引发套接字退出循环并让进程结束?

套接字代码在……下面

[再次编辑]

$SIGNAL = 0;
        sub sigHandler{ 
            #&logData("SIGNALED");
            $SIGNAL++ ;
        }
        $SIG{"USR1"}=&;sigHandler;



        # Create a new socket,on port 9999
        my $PORT = 9999;
        print ("opening connection on port $PORT");
        $lsn = new IO::Socket::INET(Listen => 1,LocalPort => $PORT,Reuse => 1,Proto => 'tcp' );
           #or die ("Couldn't start server: $!");



        # Create an IO::Select handler
        $sel = new IO::Select( $lsn );

        # Close filehandles

        close(STDIN); close(STDOUT);

        warn "Server ready.  Waiting for connections . . .  on n";


        # Enter into while loop,listening to the handles that are available.
        # this SHOULD be an infinite loop... I don't see why it would eval to false when 
        # I send a signal to increment $SIGNAL by one.
        while( @read_ready = $sel->can_read ) {
            $MESSAGE = 0;
            $fh  = $read_ready[0];

                # Create a new socket
                if($fh == $lsn) {
                    $new = $lsn->accept;
                    $sel->add($new);
                    push( @data,fileno($new) . " has joined.");
                    warn "Connection from " . $new->peerhost . ".n";

                }

                # Handle connection
                else {

                    $input = <$fh>;
                    chomp $input;
                    warn "GOT INPUT '$input'n";

                    if($input eq "<policy-file-request/>"){
                        $MESSAGE = 
                            qq~<?xml version="1.0"?>
                            <cross-domain-policy>
                              <allow-access-from domain="*" to-ports="*"/>
                            </cross-domain-policy>~;
                        $SIGNAL++;
                    }


                    if ( $input eq '') {#disconnection notification by client

                        warn "Disconnection from " . $new->peerhost . ".n";                
                        $sel->remove($fh);
                        $fh->close;
                    }
                    if ( $input eq 'READY'){
                        warn "CLIENT READY = 1n";
                        $CLIENT_READY = 1;
                    }

                }



            # Write to the clients that are available
            foreach $fh ( @write_ready = $sel->can_write(0) ) {
                if($MESSAGE == 0){
                    #set message here based on criteria
                    $MESSAGE = "UPDATE";
                }
                warn "outside send ifn";
                if($CLIENT_READY == 1 && $SIGNAL > 0){
                    warn ("sending $MESSAGE to $fhn");         
                    $CLIENT_READY = 0;          
                    $SIGNAL--;
                    print $fh "$MESSAGE" or warn "can't send message to $fh";  
                }             
            }
        }


        warn "Server ended.n";

解决方法

如果在perl脚本中安装了 signal handler,则不必结束脚本. (不要通过调用die退出处理程序)注意,您不需要发送SIGINT或SIGKILL,也可以发送SIGUSR1.

编辑
你的kill命令的命令行后面有一个逗号,后面不应该有-USR1,(它是kill -USR1 4169)

编辑2
while(can_read)循环可能在它被信号中断时接收到空文件句柄数组时存在.您可以通过以下条件来防止这种情况:

while ((@read_ready = $sel->can_read) || 0 < $sel->count ) {

并更新循环以处理空的@ready数组.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读