如何在 perl 脚本中生成其他程序并立即继续 Perl 处理?
如何在 perl 脚本中生成其他程序并立即继续 Perl 处理(而不是暂停直到生成的程序终止)? 是否可以在生成的程序运行时处理来自该程序的输出,而不是等待它结束?
How to spawn other programs within perl script and immediately continue Perl processing (instead of halting until the spawned program terminates) ?
Is it possible to process the output coming from the spawned program while it is running instead of waiting it to end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
open
(使用两个命令行参数运行程序/bin/some/program
):从
$fh
读取将为您提供正在运行的程序的标准输出。另一种方法也有效:
这会将您在文件句柄上写入的所有内容通过管道传输到生成进程的标准输入。
如果您想在同一进程中读取和写入,请查看
IPC::Open3
和IPC::Cmd
模块。You can use
open
for this (to run the program/bin/some/program
with two command-line arguments):Reading from
$fh
will give you the stdout of the program you're running.The other way around works as well:
This pipes everything you write on the filehandle to the stdin of the spawned process.
If you want to read and write to and from the same process, have a look at the
IPC::Open3
andIPC::Cmd
modules.要在后台运行程序并“继续”,您所要做的就是添加“&”在命令末尾(我假设您使用的是 Linux)。例如:
system("command &");
请注意system("command", "arg1", "&");
不起作用,因为它会通过“&”到命令而不是 shell。您可以通过执行以下操作简单地打印命令的输出:print system("command");
To run a program in the background and "continue going" all you have to do is add "&" at the end of the command (I'm assuming you are using Linux). example:
system("command &");
note thatsystem("command", "arg1", "&");
will NOT work, since it will pass the "&" to the command and not to the shell. You can simply print the output of a command by doing:print system("command");