使用子进程,pty和线程池的死锁

问题描述 投票:4回答:1

[在特殊情况下,我想将tty伪造为在ThreadPoolExecutor中运行的子流程(想像xargs -p)并捕获输出。

我创建了以下内容,似乎可以连续工作:

import contextlib
import concurrent.futures
import errno
import os
import subprocess
import termios


@contextlib.contextmanager
def pty():
    r, w = os.openpty()
    try:
        yield r, w
    finally:
        for fd in r, w:
            try:
                os.close(fd)
            except OSError:
                pass


def cmd_output_p(*cmd):
    with pty() as (r, w):
        proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=w, stderr=w)
        os.close(w)
        proc.stdin.close()

        buf = b''
        while True:
            try:
                bts = os.read(r, 1024)
            except OSError as e:
                if e.errno == errno.EIO:
                    bts = b''
                else:
                    raise
            else:
                buf += bts
            if not bts:
                break

    return proc.wait(), buf

和一些示例用法:

>>> cmd_output_p('python', '-c', 'import sys; print(sys.stdout.isatty())')
(0, b'True\r\n')

太棒了!但是,当我在concurrent.futures.ThreadPoolExecutor中运行相同的过程时,会出现很多故障模式(还有另外一种较为罕见的故障模式,其中OSError: [Errno 9] Bad file descriptor出现在看似随机的代码行上,但是我没有为那])>

例如以下代码:

def run(arg):
    print(cmd_output_p('echo', arg))


with concurrent.futures.ThreadPoolExecutor(5) as exe:
    exe.map(run, ('1',) * 1000)

这会经过一系列过程,然后最终挂起。发出^C给出以下堆栈跟踪(需要两次^ C才能结束进程)

$ python3 t.py
...

(0, b'1\r\n')
(0, b'1\r\n')
(0, b'1\r\n')
^CTraceback (most recent call last):
  File "t.py", line 49, in <module>
    exe.map(run, ('1',) * 1000)
  File "/usr/lib/python3.6/concurrent/futures/_base.py", line 611, in __exit__
    self.shutdown(wait=True)
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 152, in shutdown
    t.join()
  File "/usr/lib/python3.6/threading.py", line 1056, in join
    self._wait_for_tstate_lock()
  File "/usr/lib/python3.6/threading.py", line 1072, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt
^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 40, in _python_exit
    t.join()
  File "/usr/lib/python3.6/threading.py", line 1056, in join
    self._wait_for_tstate_lock()
  File "/usr/lib/python3.6/threading.py", line 1072, in _wait_for_tstate_lock
    elif lock.acquire(block, timeout):
KeyboardInterrupt

大概是我做错了,但是我发现与子进程/ pty交互的所有示例都执行相同的步骤-如何防止这种死锁?

我有一个特殊的情况,我想将tty伪造为在ThreadPoolExecutor中运行的子进程(想像xargs -p)并捕获输出。我创建了以下内容,似乎...

python subprocess deadlock pty
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.