Skip to main content
 首页 » 编程设计

python之在 multiprocessing.Process() 中调用 subprocess.Popen() 时管道损坏

2025年05月04日11pengyingh

我在从 multiprocessing.Process() 中进行 shell 调用时遇到问题。该错误似乎来自 Git,但我无法弄清楚为什么它只发生在 multiprocessing.Process() 中。请注意,下面是一个演示正在发生的事情的示例......在实际代码中,Process() 中还有很多事情......但我正在使用 Popen 进行 shell 调用作为它的一部分:

#!/usr/bin/python 
 
import os 
import shutil 
from multiprocessing import Process 
from subprocess import Popen 
 
def run(): 
    cmd_args = ['git', 'clone', 'git@github.com:derks/test.git', 'test-repo-checkout'] 
    res = Popen(cmd_args) 
    res.wait() 
 
# clean 
if os.path.exists('./test-repo-checkout'): 
    shutil.rmtree('./test-repo-checkout') 
 
print "\n--- this doesnt work" 
process = Process(target=run) 
process.start() 
process.join() 
 
print "\n--- this does work" 
run() 

结果是:

$ python test.py 
 
--- this doesnt work 
Cloning into test-repo-checkout... 
Warning: untrusted X11 forwarding setup failed: xauth key data not generated 
Warning: No xauth data; using fake authentication data for X11 forwarding. 
fatal: write error: Broken pipe 
 
--- this does work 
Cloning into test-repo-checkout... 
Warning: untrusted X11 forwarding setup failed: xauth key data not generated 
Warning: No xauth data; using fake authentication data for X11 forwarding. 
remote: Counting objects: 9, done. 
remote: Compressing objects: 100% (5/5), done. 
remote: Total 9 (delta 1), reused 0 (delta 0) 
Receiving objects: 100% (9/9), done. 
Resolving deltas: 100% (1/1), done. 

任何帮助都会很棒...我对多处理模块还是陌生的,直到现在才遇到任何问题。

请您参考如下方法:

Hrm,我刚刚意识到我在 Python 2.6.1 上运行....在 2.6.4 上运行相同的示例没有有同样的问题。看起来像是已在 Python 中修复的错误。