perl 进程之间的 perl Win32 信号处理 [英] perl Win32 Signal Handling between perl processes

查看:38
本文介绍了perl 进程之间的 perl Win32 信号处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 中有几个长期运行的 perl 脚本(草莓 perl).

I have a couple long running perl scripts in windows (strawberry perl) that I'm working on.

  1. 第一个进程是父监控进程.它每 24 小时重启一次子进程,并将始终运行.
  2. 第二个是子付款处理脚本.此进程必须在关闭之前完成其正在执行的任何操作.

据我所知,信号处理 在 win32 上的 perl 中不起作用并且不应该依赖它.还有其他方法可以处理信号吗?Win32::Process::Kill 似乎在不让它安全关闭的情况下杀死进程.

It's my understanding that signal handling doesn't work in perl on win32 and that it shouldn't be relied on. Is there some other way that I can handle a signal? Win32::Process::Kill seems to kill the process without letting it safely shut down.

这是我尝试过的信号处理...

This is the signal handling that I've tried...

#Child
my $interrupted = 0;
$SIG{INT} = sub{$interrupted = 1;};
while(!$interrupted){
  #keep doing your thing, man
}

#Parent
my $pid = open2(\*CHLD_OUT,\*CHLD_IN,'C:\\strawberry\\perl\\bin\\perl.exe','process.pl');
kill INT=>$pid;
waitpid($pid,0);

我唯一能想到的就是在两个进程之间打开一个套接字,并通过该套接字写入消息.但一定有更容易的事情.有人知道可以做到这一点的任何模块吗?

The only other thing I can think of is to open a socket between the two processes and write messages across the socket. But there must be something easier. Anyone know of any module that can do this?

更新

我已经开始致力于通过 IO::Socket::INETIO::Select 创建信号"机制打开套接字.这似乎有效,我正在考虑编写一个与 AnyEvent 兼容的模块.但是我仍然对不需要打开侦听端口并且不需要服务器/客户端关系的实现感兴趣.是否可以通过在 Windows 中订阅和触发自定义事件来做到这一点?

I've started working on creating a "signal" mechanism via IO::Socket::INET and IO::Select by opening a socket. This appears to work and I'm thinking of writing a module that is compatible with AnyEvent. But I'm still interested in an implementation that doesn't require opening a listening port and that doesn't require a server/client relationship. Is it possible to do this by subscribing to and firing custom events in windows?

推荐答案

使用 ActiveState Perl 我通过 Win32::Event 模块.

With ActiveState Perl I use windows native events through the Win32::Event module.

这样你就不需要实现任何花哨的东西,你甚至可以让你的脚本与本机应用程序交互.我在许多应用程序中使用这个模块.

This way you don't need to implement anything fancy and you can even have your script interract with native applications. I use this module in many applications.

因为它是 Win32::IPC 的一部分,它使用原生代码,它可能不适用于 Strawberry Perl.如果是这种情况,您可以尝试从 CPAN 来源编译它.如果您有很多基于 Windows perl 的软件,可能值得一试.

Since it is part of Win32::IPC, which uses native code, it may not be available for Strawberry Perl. If that is the case you could try compiling it from the CPAN sources. It might be worth a try if you have lots of Windows perl-based software.

这篇关于perl 进程之间的 perl Win32 信号处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆