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

时间:2013-01-23 21:15:29

标签: perl zeromq plack starman

我有以下代码。我想在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
}

1 个答案:

答案 0 :(得分:2)

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

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

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

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