gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:24:38 24 4
gpt4 key购买 nike

好的,所以,我得到了信号,但由于某种原因,该进程在收到信号后仍然存在。

如果我在创建套接字之前添加一个无限循环 (while(1) ),那么它会按规定工作。
所以......当发出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>\0~;
$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 = 1\n";
$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 if\n";
if($CLIENT_READY == 1 && $SIGNAL > 0){
warn ("sending $MESSAGE to $fh\n");
$CLIENT_READY = 0;
$SIGNAL--;
print $fh "$MESSAGE\0" or warn "can't send message to $fh";
}
}
}


warn "Server ended.\n";

最佳答案

我不明白我是否误读了您的问题,或者您是否忽略了显而易见的问题。我不会依赖信号来做这样的事情。

我有 scriptA监听套接字并有 scriptBscriptA 发消息(而不是信号)。如果它收到正确的消息,它会将相关数据写出到所有连接到它的客户端。

关于perl - 如何从另一个进程调用正在运行的 Perl 进程中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1991474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com