- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Unix 中工作,我有一个“通用工具”,可以在后台加载另一个进程(GUI 实用程序),然后退出。
我使用 Popen 和 proc.communicate()
方法从 Python 脚本调用我的“通用工具”。
我的“通用工具”运行约 1 秒,在后台加载 GUI 进程并立即退出。
问题是 proc.communicate()
继续等待进程,尽管它已经终止。我必须手动关闭 GUI(这是在 BG 上运行的子进程),因此 proc.communicate() 返回。
如何解决这个问题?
我需要 proc.communicate() 在主进程终止后返回,而不是等待在后台运行的子进程...
谢谢!!!
添加一些代码片段:
我的“通用工具”最后主线(用 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...'
最佳答案
不要使用沟通
。通信被明确设计为等待进程的标准输出关闭。据推测,perl 不会关闭标准输出,因为它将它保持打开状态以供其自己的子进程写入。
您也并不真正需要使用 Popen
,因为您并没有真正使用它的功能。也就是说,您创建管道,然后使用您自己的消息重新打印到标准输出。而且看起来您根本不需要 shell。
尝试使用subprocess.call
甚至subprocess.check_call
。
例如。
subprocess.check_call(cmd)
无需检查返回值,因为如果进程以非零退出代码返回,则 check_call
会引发异常(其中包含退出代码)。进程的输出直接写入控制终端——无需重定向输出。
最后,如果 cmd
是可执行文件路径及其参数的组合,则使用 shlex.split
。
例如。
cmd = "echo whoop" # or cmd = "ls 'does not exist'"
subprocess.check_call(shlex.split(cmd))
用于测试的示例代码:
mypython.py
import subprocess, shlex
subprocess.check_call(shlex.split("perl myperl.pl"))
print("finishing top level process")
myperl.pl
print "starting perl subprocess\n";
my $cmd = 'python -c "
import time
print(\'starting python subprocess\')
time.sleep(3);
print(\'finishing python subprocess\')
" &';
system($cmd);
print "finishing perl subprocess\n";
输出是:
$ python mypython.py
starting perl subprocess
finishing perl subprocess
finishing top level process
$ starting python subprocess
finishing python subprocess
关于Python:Popen - 等待主进程,但不等待后台子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25848265/
我正在使用java引擎来处理一些内容。 我想知道这样会占用不必要的资源吗? command = 'some command to run my jar file' p = subprocess.Pop
我正在尝试使用以下代码块将一个进程的输出作为输入发送到另一个进程: p1 = Popen(command, stdout=PIPE) p2 = Popen(dgxmcmd, stdin=p1.stdo
popen 缓冲输出而系统没有。这是唯一的区别吗? 我知道 popen 和 system 都通过 shell 运行命令。但是,popen() 是否为 evil作为系统()? 最佳答案 看,从本质上讲,
用代码最容易解释: require 'timeout' puts "this block will properly kill the sleep after a second" IO.popen("
似乎既执行子进程又创建管道进行输入/输出,只是 subprocess 较新。 我的问题是,有没有subprocess.Popen可以做而os.popen不能做的功能,所以我们需要新模块subproce
我有一个生成以下输出的程序: ┌───────────────────────┐ │10 day weather forecast│
我正在使用以下命令来运行 shell 命令(创建子进程): cmd = "ls" process = subprocess.Popen(cmd, shell=True, stdout=subproce
得到结果后,我需要停止通过Python中的Popen发出的服务(在另一个线程的后台运行),但是以下方法失败了(只是使用ping)解释): class sample(threading.Thread):
Python - os.popen 和 subprocess.Popen 有什么区别? 最佳答案 os 进程功能被认为已过时。 subprocess 模块是在 Python 2.4 中引入的,作为与子
根据 python 文档 http://docs.python.org/library/subprocess.html ,建议将 os.popen 替换为 Popen 类,现在我有以下命令: impo
非常具体的问题(我希望):以下三个代码有什么区别? (我希望它只是第一个不等待子进程完成,而第二个和第三个会这样做。但我需要确定这是 only 的区别...) 我也欢迎其他评论/建议(尽管我已经很清楚
我有以下代码: pwd = '/home/user/svnexport/Repo/' updateSVN = "svn up " + pwd cmd = os.popen(updateSVN) get
我正在尝试编写简单的 c 函数,这将使我有可能看到从一个流到另一个流的数据传输进度,并将其显示在我的字符 LCD 上。 我设法传输数据并指示进度,但如何获得管道的结果? 所以基本上我想在 c 中做对应
我正在尝试使用 subprocess 模块与使用 Python 的命令行聊天机器人进行通信。 (http://howie.sourceforge.net/使用编译后的 win32 二进制文件,我有我的
我需要为 Ghostscript 创建一个 Monkey 补丁,我必须从 os.popen 迁移到 subsession.popen 因为我不能在我的系统中使用 shell . 我这样试过: def
在 Linux 操作系统上,下面的 python 代码提供了当前目录中的目录。 dirs = os.popen('ls -d */').read().split(os.linesep) print d
当我们从 Python 2.7.3 升级到 Python 2.7.5 时,大量使用 subprocess.Popen() 的内部库的自动化测试开始失败。该库用于线程环境。调试问题后,我能够创建一个简短
我无法得到它与 bash 相关或 python 子进程,但结果不同: >>> subprocess.Popen("echo $HOME", shell=True, stdout=subprocess.
这里我想执行一个命令,我必须在执行第一个命令后给这个命令输入。 我想执行 obex_test蓝牙模式命令而不是在我必须为启动服务器提供像's'这样的输入之后我怎么能给这个东西。这是我的代码,我写了这个
在我的 Lua 程序中,我必须捕获来自外部程序的输出。这个外部程序需要某些环境变量。所以我这样做: e = "" e = e .. "A=100;" e = e .. "B=Hi;" e = e ..
我是一名优秀的程序员,十分优秀!