【发布时间】:2014-11-08 23:49:19
【问题描述】:
我在 Unix 中工作,我有一个“通用工具”,可以在后台加载另一个进程(GUI 实用程序)并退出。
我从 Python 脚本调用我的“通用工具”,使用 Popen 和 proc.communicate() 方法。
我的“通用工具”运行约 1 秒,在后台加载 GUI 进程并立即退出。
问题是proc.communicate() 继续等待进程,尽管它已经终止。我必须手动关闭 GUI(这是在 BG 上运行的子进程),所以 proc.communicate() 返回。
如何解决?
我需要proc.communicate()在主进程终止后返回,而不是等待在后台运行的子进程...
谢谢!!!
编辑:
添加一些代码sn-ps:
我的“通用工具”最后几行(用 Perl 编写):
if ($args->{"gui"}) {
my $script_abs_path = abs_path($0);
my $script_dir = dirname($script_abs_path);
my $gui_util_path = $script_dir . "/bgutil";
system("$gui_util_path $args->{'work_area'} &");
}
return 0;
我运行“通用工具”的 Python 脚本:
cmd = PATH_TO_MY_GENERAL_TOOL
proc = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
stdout, dummy = proc.communicate()
exit_code = proc.returncode
if exit_code != 0:
print 'The tool has failed with status: {0}. Error message is:\n{1}'.format(exit_code, stdout)
sys.exit(1)
print 'This line is printed only when the GUI process is terminated...'
【问题讨论】:
-
向我们展示您的代码不会有什么坏处。
-
是的,分享一些代码!
-
你确定通用工具已经终止了吗?我的意思是,如果您运行
ps(在 Linux - Unix 上)或任务管理器(在 Windows 上),它是否仍然存在? -
进程应该被终止...我将分享我的代码...
标签: python subprocess