繁体   English   中英

subprocess.check_output()的问题

[英]Trouble with subprocess.check_output()

我使用subprocess.check_output()遇到了一些奇怪的问题。 起初我只是使用subprocess.call() ,一切正常。 但是,当我只是为check_output()切换call() check_output() ,我收到一个奇怪的错误。

代码之前(工作正常):

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

代码后(不起作用):

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

这给出了错误: 错误

我无法重定向这个,因为程序挂起来,我不能ctrl-c出来。 任何想法为什么会这样? subprocess.call()和check_output()之间可能导致这种情况有什么区别?

以下是包含多处理部分的附加代码:

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()

谢谢!

您遇到了Python Issue 9400

关于subprocess.call()subprocess.check_output()你必须要了解一个关键的区别。 subprocess.call()将执行您提供的命令,然后为您提供返回代码 另一方面, subprocess.check_output()以字符串形式返回程序的输出,但它会尝试帮助你并检查程序的返回代码并在程序未执行时引发异常subprocess.CalledProcessError )成功(返回非零返回码)。

当您使用多处理池调用pool.map()时,它将尝试将子pool.map()异常传播回main并在那里引发异常。 显然,如何定义subprocess.CalledProcessError异常类存在问题,因此当多处理库尝试将异常传播回main时,pickling会失败。

你调用返回一个非零返回码的程序,这使得subprocess.check_output()抛出一个异常,并且pool.map()不能正确处理它,所以你得到的TypeError从失败的尝试结果检索异常。

作为旁注, subprocess.CalledProcessError的定义必须搞砸,因为如果我打开我的2.7.6终端,导入子进程,并且手动引发错误,我仍然得到TypeError ,所以我不认为它只是酸洗问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM