如何在 async def 函数中正确使用 asyncio.create_subprocess_shell() ?
How do I properly use asyncio.create_subprocess_shell() in an async def function?
我正在尝试编写一个简单的子进程程序,它将调用一个 long-运行ning shell 命令,允许其他进程 运行,然后执行一些干净的- 完成后的任务。
不幸的是,我 运行 遇到错误只是让 shell 命令在异步事件循环中正确执行。行为是它看起来 python 从不等待 shell 脚本完成 运行ning。我知道 shell 脚本可以正常工作,因为我可以从提示中手动 运行 它。我 运行ning 的 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
我正在 运行宁 python v3.5.2 Ubuntu 16.04.
更新
根据下面 Sam 的评论,我需要将我的代码更新为以下内容才能正常工作:
process = await asyncio.create_subprocess_shell(cmd1, stdin = PIPE, stdout PIPE, stderr = STDOUT)
await process.wait()
这是对他的代码稍作修改,但有效。
问题是没有等待进程完成;你只需要等待它开始。
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.")
我正在尝试编写一个简单的子进程程序,它将调用一个 long-运行ning shell 命令,允许其他进程 运行,然后执行一些干净的- 完成后的任务。
不幸的是,我 运行 遇到错误只是让 shell 命令在异步事件循环中正确执行。行为是它看起来 python 从不等待 shell 脚本完成 运行ning。我知道 shell 脚本可以正常工作,因为我可以从提示中手动 运行 它。我 运行ning 的 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
我正在 运行宁 python v3.5.2 Ubuntu 16.04.
更新
根据下面 Sam 的评论,我需要将我的代码更新为以下内容才能正常工作:
process = await asyncio.create_subprocess_shell(cmd1, stdin = PIPE, stdout PIPE, stderr = STDOUT)
await process.wait()
这是对他的代码稍作修改,但有效。
问题是没有等待进程完成;你只需要等待它开始。
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.")