Python - 将 perl 作为子进程调用 - 等待完成其后台进程并打印到 shell
Python - call perl as subprocess - wait for finish its background process and print to shell
我有一个 Perl 脚本,它在内部调用 system("something &")
。如果我从 shell 运行 调用脚本,然后使用 wait
命令等待所有后台进程。
如何从 Python 调用此脚本并等待它生成的“后台”脚本完成?
我试过了
pipe = subprocess.Popen(["perl", "script.pl", "data1", "data2"], stdin=subprocess.PIPE)
pipe.communicate()
但它不会等待并在 perl 脚本完成后退出。
另一个问题是我不知道如何在 运行ning 时将后台进程的输出打印到 shell。
我已根据此答案更改 Perl 脚本来解决此问题 How can I make Perl wait for child processes started in the background with system()?
我变了
system("something &")
到
my $pid = fork();
if ($pid == -1) {
print("Failed to fork");
die;
} elsif ($pid == 0) {
exec "something ";
}
并添加
while (wait() != -1) {}
脚本结束
我有一个 Perl 脚本,它在内部调用 system("something &")
。如果我从 shell 运行 调用脚本,然后使用 wait
命令等待所有后台进程。
如何从 Python 调用此脚本并等待它生成的“后台”脚本完成?
我试过了
pipe = subprocess.Popen(["perl", "script.pl", "data1", "data2"], stdin=subprocess.PIPE)
pipe.communicate()
但它不会等待并在 perl 脚本完成后退出。
另一个问题是我不知道如何在 运行ning 时将后台进程的输出打印到 shell。
我已根据此答案更改 Perl 脚本来解决此问题 How can I make Perl wait for child processes started in the background with system()?
我变了
system("something &")
到
my $pid = fork();
if ($pid == -1) {
print("Failed to fork");
die;
} elsif ($pid == 0) {
exec "something ";
}
并添加
while (wait() != -1) {}
脚本结束