I am new with Python programming. I want to call a few shell commands in parallel and get their result accumulated in a single array just like javascript promise.all()
method.
I have the following code in 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())
I am getting the following error and not able to find any solution, please assist Thanks.
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
It's a planned feature but it is not implemented yet. It will work in the future but the current versions don't support it.
I looked through the other branches and none of them are implemented. The subprocess module ( https://docs.python.org/3/library/subprocess.html ) is widely known and the asyncio
support for it is not yet finished. The endpoints were defined but they are as of now not usable.
It is explained in the docs:
https://docs.python.org/3.7/library/asyncio-platforms.html#asyncio-windows-subprocess
SelectorEventLoop on Windows does not support subproceses. On Windows, ProactorEventLoop should be used instead:
import asyncio
asyncio.set_event_loop_policy(
asyncio.WindowsProactorEventLoopPolicy())
asyncio.run(your_code())
The default event loop on Windows is now the ProactorEventLoop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.