在multiprocessing.Process()中调用subprocess.Popen()时出现Broken Pipe

2 投票
1 回答
1132 浏览
提问于 2025-04-16 04:52

我在使用 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.

如果能得到一些帮助那就太好了……我对 multiprocessing 模块还很陌生,直到现在还没有遇到过什么问题。

1 个回答

1

嗯,我刚发现我在用的是Python 2.6.1版本……在2.6.4版本上运行同样的例子就没有这个问题了。看起来这是Python里的一个错误,已经被修复了。

撰写回答