[英]Trouble with subprocess.check_output()
I'm having some strange issues using subprocess.check_output()
. 我使用subprocess.check_output()
遇到了一些奇怪的问题。 At first I was just using subprocess.call()
and everything was working fine. 起初我只是使用subprocess.call()
,一切正常。 However when I simply switch out call()
for check_output()
, I receive a strange error. 但是,当我只是为check_output()
切换call()
check_output()
,我收到一个奇怪的错误。
Before code (works fine): 代码之前(工作正常):
def execute(hosts):
''' Using psexec, execute the batch script on the list of hosts '''
successes = []
wd = r'c:\\'
file = r'c:\\script.exe'
for host in hosts:
res = subprocess.call(shlex.split(r'psexec \\\\%s -e -s -d -w %s %s' % (host,wd,file)))
if res.... # Want to check the output here
successes.append(host)
return successes
After code (doesn't work): 代码后(不起作用):
def execute(hosts):
''' Using psexec, execute the batch script on the list of hosts '''
successes = []
wd = r'c:\\'
file = r'c:\\script.exe'
for host in hosts:
res = subprocess.check_output(shlex.split(r'psexec \\\\%s -e -s -d -w %s %s' % (host,wd,file)))
if res.... # Want to check the output here
successes.append(host)
return successes
This gives the error: 这给出了错误:
I couldnt redirect this because the program hangs here and I can't ctrl-c out. 我无法重定向这个,因为程序挂起来,我不能ctrl-c出来。 Any ideas why this is happening? 任何想法为什么会这样? What's the difference between subprocess.call() and check_output() that could be causing this? subprocess.call()和check_output()之间可能导致这种情况有什么区别?
Here is the additional code including the multiprocessing portion: 以下是包含多处理部分的附加代码:
PROCESSES = 2
host_sublists_execute = [.... list of hosts ... ]
poolE = multiprocessing.Pool(processes=PROCESSES)
success_executions = poolE.map(execute,host_sublists_execute)
success_executions = [entry for sub in success_executions for entry in sub]
poolE.close()
poolE.join()
Thanks! 谢谢!
You are encountering Python Issue 9400 . 您遇到了Python Issue 9400 。
There is a key distinction you have to understand about subprocess.call()
vs subprocess.check_output()
. 关于subprocess.call()
和subprocess.check_output()
你必须要了解一个关键的区别。 subprocess.call()
will execute the command you give it, then provide you with the return code . subprocess.call()
将执行您提供的命令,然后为您提供返回代码 。 On the other hand, subprocess.check_output()
returns the program's output to you in a string, but it tries to do you a favor and check the program's return code and will raise an exception ( subprocess.CalledProcessError
) if the program did not execute successfully (returned a non-zero return code). 另一方面, subprocess.check_output()
以字符串形式返回程序的输出,但它会尝试帮助你并检查程序的返回代码并在程序未执行时引发异常 ( subprocess.CalledProcessError
)成功(返回非零返回码)。
When you call pool.map()
with a multiprocessing pool, it will try to propagate exceptions in the subprocesses back to main and raise an exception there. 当您使用多处理池调用pool.map()
时,它将尝试将子pool.map()
异常传播回main并在那里引发异常。 Apparently there is an issue with how the subprocess.CalledProcessError
exception class is defined, so the pickling fails when the multiprocessing library tries to propagate the exception back to main. 显然,如何定义subprocess.CalledProcessError
异常类存在问题,因此当多处理库尝试将异常传播回main时,pickling会失败。
The program you're calling is returning a non-zero return code, which makes subprocess.check_output()
raise an exception, and pool.map()
can't handle it properly, so you get the TypeError
that results from the failed attempt to retrieve the exception. 你调用返回一个非零返回码的程序,这使得subprocess.check_output()
抛出一个异常,并且pool.map()
不能正确处理它,所以你得到的TypeError
从失败的尝试结果检索异常。
As a side note, the definition of subprocess.CalledProcessError
must be really screwed up, because if I open my 2.7.6 terminal, import subprocess, and manuallly raise the error, I still get the TypeError
, so I don't think it's merely a pickling problem. 作为旁注, subprocess.CalledProcessError
的定义必须搞砸,因为如果我打开我的2.7.6终端,导入子进程,并且手动引发错误,我仍然得到TypeError
,所以我不认为它只是酸洗问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.