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

perl – 当Starman收到HUP时,ZMQ套接字阻塞

发布时间:2020-12-16 06:14:51 所属栏目:大数据 来源:网络整理
导读:我有以下代码.我想在starman服务器收到HUP信号时调用$pub- close方法. 我怎么知道子进程结束了? 我可以使用END {}块吗?我尝试了这个,当plackup重新启动(编辑后)似乎工作.我和starman一起试过了.我发送了HUP信号,但孩子们没有重新启动. 我应该为HUP安装信号
我有以下代码.我想在starman服务器收到HUP信号时调用$pub-> close方法.

>我怎么知道子进程结束了?
>我可以使用END {}块吗?我尝试了这个,当plackup重新启动(编辑后)似乎工作.我和starman一起试过了.我发送了HUP信号,但孩子们没有重新启动.
>我应该为HUP安装信号处理程序吗?这是如何运作的?

我希望在孩子重新启动之前清理,如果我没有,子进程将阻止.

这是我使用的.psgi文件.

use ZMQ;
use ZMQ::Constants ':all';
use Plack::Builder;

our $ctx = ZMQ::Context->new(1);
my $pub = $ctx->socket(ZMQ_PUB);
$pub->bind('tcp://127.0.0.1:5998');

# I want to close the socket and terminate the context
# when the server is restarted with kill -HUP pid
# It seems the children won't restart because the sockets isn't closed.
# The next two lines should be called before the child process ends.

# $pub->close;
# $ctx->term;

builder {
    $app
}

解决方法

PSGI应用程序没有标准的方法来注册每个进程的清理处理程序,而Starman似乎没有实现任何可直接使用的东西.但是,当进程退出时,你可以通过修补Starman来运行一些代码.

由于Starman基于Net :: Server :: PreFork并且不使用child_finish_hook()本身,您可以通过将此插入到.psgi文件中来覆盖此Net :: Server :: PreFork挂钩:

sub Starman::Server::child_finish_hook {
    $pub->close();
    $ctx->term();
}

ZMQ内部使用线程可能会以某种方式阻止使用END块进行清理(或仅依赖于全局析构函数),我认为将信号处理留给Net :: Server框架是最明智的.

(编辑:李大同)

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

    推荐文章
      热点阅读