如何指定 Perl 系统调用的超时限制?
有时我的系统调用会进入永无止境的状态。为了避免我希望能够在指定的时间后中断通话。
有没有办法指定 system
的超时限制?
system("command", "arg1", "arg2", "arg3");
我希望从 Perl 代码中实现超时以实现可移植性,而不是使用 ulimit 等某些操作系统特定的函数。
Sometimes my system call goes into a never ending state. To, avoid that I want to be able to break out of the call after a specified amount of time.
Is there a way to specify a timeout limit to system
?
system("command", "arg1", "arg2", "arg3");
I want the timeout to be implemented from within Perl code for portability, and not using some OS specific functions like ulimit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅
alarm
函数。 pod 中的示例:CPAN 上有一些模块可以更好地包装这些模块,例如:
时间::输出
See the
alarm
function. Example from pod:There are modules on CPAN which wrap these up a bit more nicely, for eg:
Time::Out
您可以使用 IPC::Run 的 run 方法代替系统。并设置一个超时时间。
You can use IPC::Run's run method instead of system. and set a timeout.
我之前刚刚在 Perl + Linux 中使用过 timeout 命令,您可以这样测试:
4.317 秒后输出:
timeout 命令是所有主要“正常”命令的一部分“ Linux 发行版是 coreutils 的一部分。
I've just used the
timeout
command in Perl + Linux before, something you can test like this:Output after 4.317 seconds:
The
timeout
command is part of all major "normal" Linux distributions a.f.a.i.k, a part of coreutils.System::Timeout 怎么样?
How about System::Timeout ?