我有以下代码.我想在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框架是最明智的.