gpt4 book ai didi

Python 异步 : Running subprocess_exec on a worker thread

转载 作者:IT王子 更新时间:2023-10-29 01:20:49 31 4
gpt4 key购买 nike

所以我使用 Python asyncio 模块(在 Linux 上)启动子进程,然后异步监视它。我的代码工作正常...在主线程 上运行时。但是当我在工作线程上运行它时,它挂起,并且永远不会调用 process_exited 回调。

我怀疑这实际上可能是某种未记录的缺陷或在工作线程上运行 subprocess_exec 的问题,可能与实现如何处理后台线程中的信号有关。但也可能是我把事情搞砸了。

一个简单的、可重现的例子如下:

class MyProtocol(asyncio.SubprocessProtocol):
def __init__(self, done_future):
super().__init__()
self._done_future = done_future

def pipe_data_received(self, fd, data):
print("Received:", len(data))

def process_exited(self):
print("PROCESS EXITED!")
self._done_future.set_result(None)

def run(loop):
done_future = asyncio.Future(loop = loop)
transport = None
try:
transport, protocol = yield from loop.subprocess_exec(
lambda : MyProtocol(done_future),
"ls",
"-lh",
stdin = None
)
yield from done_future
finally:
if transport: transport.close()

return done_future.result()

def run_loop():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) # bind event loop to current thread

try:
return loop.run_until_complete(run(loop))
finally:
loop.close()

所以在这里,我设置了一个 asyncio 事件循环来执行 shell 命令 ls -lh,然后在从子进程接收到数据时触发回调,并且子进程退出时的另一个回调。

如果我只是在 Python 程序的主线程中直接调用 run_loop(),一切都会很顺利。但如果我说:

t = threading.Thread(target = run_loop)
t.start()
t.join()

然后发生的事情是成功调用了 pipe_data_received() 回调,但是从未调用过 process_exited(),程序只是挂起。

在谷歌搜索并查看用于实现 unix_events.pyasyncio 源代码之后,我发现可能需要手动将我的事件循环附加到全局“child watcher”对象,如下:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop) # bind event loop to current thread
asyncio.get_child_watcher().attach_loop(loop)

显然,子监视器是一个(未记录的)对象,负责在幕后调用 waitpid(或类似的东西)。但是当我尝试这样做并在后台线程中运行 run_event_loop() 时,我得到了错误:

  File "/usr/lib/python3.4/asyncio/unix_events.py", line 77, in add_signal_handler
raise RuntimeError(str(exc))
RuntimeError: set_wakeup_fd only works in main thread

所以这里看起来实现实际上做了一个检查以确保信号处理程序只能在主线程上使用,这让我相信在当前的实现中,使用 subprocess_exec事实上,如果不更改 Python 源代码本身,后台线程是根本不可能的。

我对此是否正确?遗憾的是,asyncio 模块的文档非常少,所以我很难对我在这里的结论充满信心。我可能只是做错了什么。

最佳答案

只要 asyncio 循环在主线程中运行并实例化其子观察者,就可以在工作线程中处理子进程:

asyncio.get_child_watcher()
loop = asyncio.get_event_loop()
coro = loop.run_in_executor(None, run_loop)
loop.run_until_complete(coro)

参见 this postdocumentation .

关于Python 异步 : Running subprocess_exec on a worker thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44684898/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com