小编jan*_*and的帖子

当Popen出错时,子进程的Popen关闭另一个线程中使用的stdout/stderr文件描述符

当我们从Python 2.7.3升级到Python 2.7.5时,大量使用subprocess.Popen()的内部库开始失败其自动化测试.该库用于线程环境.在调试问题之后,我能够创建一个简短的Python脚本,演示在失败的测试中看到的错误.

这是脚本(称为"threadedsubprocess.py"):

import time
import threading
import subprocess

def subprocesscall():
    p = subprocess.Popen(
        ['ls', '-l'],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        )
    time.sleep(2) # simulate the Popen call takes some time to complete.
    out, err = p.communicate()
    print 'succeeding command in thread:', threading.current_thread().ident

def failingsubprocesscall():
    try:
        p = subprocess.Popen(
            ['thiscommandsurelydoesnotexist'],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            )
    except Exception as e:
        print 'failing command:', e, 'in thread:', threading.current_thread().ident

print 'main thread is:', threading.current_thread().ident

subprocesscall_thread = threading.Thread(target=subprocesscall)
subprocesscall_thread.start()
failingsubprocesscall()
subprocesscall_thread.join()
Run Code Online (Sandbox Code Playgroud)

注意:从Python 2.7.3运行时,此脚本不会以IOError退出.从Python 2.7.5(同一个Ubuntu 12.04 64位VM)运行时,它至少失败了50%. …

python multithreading python-2.7

11
推荐指数
1
解决办法
5006
查看次数

标签 统计

multithreading ×1

python ×1

python-2.7 ×1