如何在异步def函数中正确使用asyncio.create_subprocess_shell()?

时间:2017-04-29 03:52:59

标签: python-3.x python-asyncio

我试图编写一个简单的子进程程序,它将调用一个长时间运行的shell命令,允许其他进程运行,然后在完成后执行一些清理任务。

不幸的是,我发现错误只是让shell命令在asyncio事件循环中正确执行。行为是看起来它似乎python永远不会等待shell脚本完成运行。我知道shell脚本可以工作,因为我可以从提示符手动运行它。我运行的shell脚本应该在大约3-5分钟内执行。

这是我的示例程序:

import asyncio 
from asyncio.subprocess import PIPE, STDOUT 
import subprocess 
import signal


def signal_handler(signal, frame):
    loop.stop()
    client.close()
    sys.exit(0)

async def run_async(loop = ''):
    cmd = 'sudo long_running_cmd --opt1=AAAA --opt2=BBBB'

    print ("[INFO] Starting script...")
    await asyncio.create_subprocess_shell(cmd1, stdin = PIPE, stdout = PIPE, stderr = STDOUT)
    print("[INFO] Script is complete.")


loop = asyncio.get_event_loop() 
signal.signal(signal.SIGINT, signal_handler) 
tasks = [loop.create_task(run_async())] 
wait_tasks = asyncio.wait(tasks) 
loop.run_until_complete(wait_tasks)

loop.close()

程序几乎瞬间运行并失败。此代码生成的错误是:

[INFO] Starting script...
[INFO] Script is complete.
Exception ignored in: <bound method BaseSubprocessTransport.__del__ of <_UnixSubprocessTransport closed pid=5652 running stdin=<_UnixWritePipeTransport closing fd=7 open> stdout=<_UnixReadPipeTransport fd=8 open>>>
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 126, in __del__
  File "/usr/lib/python3.5/asyncio/base_subprocess.py", line 101, in close
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 568, in close
  File "/usr/lib/python3.5/asyncio/unix_events.py", line 560, in write_eof
  File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
  File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
RuntimeError: Event loop is closed

我在Ubuntu 16.04上运行python v3.5.2。

更新

根据Sam的评论,我需要将我的代码更新为以下内容才能使其正常工作:

process = await asyncio.create_subprocess_shell(cmd1, stdin = PIPE, stdout PIPE, stderr = STDOUT)
await process.wait()

这是他的代码稍作修改,但有效。

1 个答案:

答案 0 :(得分:2)

问题在于没有任何东西等待这个过程完成;你只能等待它开始。

async def run_async(loop = ''):
    cmd = 'sudo long_running_cmd --opt1=AAAA --opt2=BBBB'

    print ("[INFO] Starting script...")
    process = await asyncio.create_subprocess_shell(cmd1, stdin = PIPE, stdout = PIPE, stderr = STDOUT)
    await process.wait()
    print("[INFO] Script is complete.")