我是Python编程的新手。我想并行调用一些shell命令,并将它们的结果累积在单个数组中,就像javascript promise.all()
方法一样。
我在Python中有以下代码
import asyncio
import os
commands = [
'netstat -n | findstr 55601',
'dir | findstr portMonitoring.py',
'ssh 10.6.100.192 netstat'
]
async def job(cmd):
# await asyncio.sleep(1)
# return "HEE"
# return os.popen(cmd).read()
process = await asyncio.create_subprocess_exec(
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
return await process.communicate()
async def main():
jobs = [job(cmd) for cmd in commands]
done, pending = await asyncio.wait(jobs, return_when=asyncio.FIRST_COMPLETED)
folders = []
[folders.append(d.result()) for d in done]
print("RESULT:", folders)
asyncio.run(main())
我收到以下错误,无法找到任何解决方案,请协助。
Traceback (most recent call last):
File "test.py", line 16, in job
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
File "C:\DEV\Python3.7.4\lib\asyncio\subprocess.py", line 217, in create_subprocess_exec
stderr=stderr, **kwds)
File "C:\DEV\Python3.7.4\lib\asyncio\base_events.py", line 1529, in subprocess_exec
bufsize, **kwargs)
File "C:\DEV\Python3.7.4\lib\asyncio\base_events.py", line 458, in _make_subprocess_transport
raise NotImplementedError
NotImplementedError
最佳答案
https://github.com/python/cpython/blob/master/Lib/asyncio/base_events.py#L493
这是一项计划的功能,但尚未实现。它将在将来运行,但是当前版本不支持它。
我浏览了其他分支,但都没有实现。子流程模块(https://docs.python.org/3/library/subprocess.html)是众所周知的,并且对它的asyncio
支持尚未完成。端点已定义,但截至目前仍无法使用。