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

发布于 2024-09-25 23:06:12 字数 1561 浏览 4 评论 0原文

我在 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', '[email protected]: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.

任何帮助真的会很棒...我对多处理模块仍然很陌生,直到现在还没有遇到任何问题。

I am having an issue when making a shell call from within a multiprocessing.Process(). The error seems to be coming from Git, but I just can't figure out why it only happens from within a multiprocessing.Process(). Note, below is an example to demonstrate what's happening... in real code there is a lot more going on within the Process()... but I'm using Popen to make shell calls as part of it:

#!/usr/bin/python

import os
import shutil
from multiprocessing import Process
from subprocess import Popen

def run():
    cmd_args = ['git', 'clone', '[email protected]: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()

The result is:

$ 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.

Any help would really be great... I'm still new to the multiprocessing module and haven't come across any issues with it until now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2024-10-02 23:06:12

嗯,我刚刚意识到我正在 Python 2.6.1 上运行......在 2.6.4 上运行相同的示例确实有同样的问题。看起来像是 Python 中修复的一个错误。

Hrm, I just realized I was running on Python 2.6.1 .... running the same example on 2.6.4 does not have the same issue. Looks like a bug that was fix in Python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文