Python's asynchronous execution material was often similar (official translation / re-editing?). It's tough for an old man who has a hard head, so I looked for an example, looked at a site in multiple languages, and finally moved by trial and error, so I tried to summarize only my liver.
All examples are external command execution patterns on Windows There is almost no commentary.
Easy and nice. This is what Ojisan came up with.
pid.py
import subprocess
from time import sleep
procs = {}
for h in servers:
p = subprocess.Popen(["some", "command"], ...)
# subprocess.run to stdout=It seems to work with PIPE, but it has not been investigated
procs[p.pid] = p
while procs:
for pid in list(procs):
if procs[pid].poll in not None:
del procs[pid]
sleep(x)
procs can be a list of p from the end, My favorite is to empty it at the end with this method.
This is OK if the target to be executed generates something. The run function is an asynchronous execution target. When I first made it, I called it with run_until_complete, so I was wondering if it would just wait with create_subprocess_shell.
async1.py
import asyncio
from asyncoio.subprocess import DEVNULL
async def run(param):
#Either of the following two works
p = await asyncio.create_subprocess_exec(*["some", "command", param],
stdout=DEVNULL, ...)
p = await asyncio.create_subprocess_shell("some command %s" % param,
stdout=DEVNULL, ...)
await p.wait()
if sys.platform.startswith('win'):
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
loop = asyncio.get_event_loop()
funcs = asyncio.gather(*[run(p) for p in parameters])
loop.run_until_complete(funcs)
If you want to do something using the standard output of the execution target, this is the case. However, the results will be returned to the main ret in the order of end, so you have to endure it.
async_stdout.py
import asyncio
from asyncoio.subprocess import PIPE
import sys
async def run(param):
p = await asyncio.create_subprocess_shell("some command %s" param,
stdout=PIPE, ...)
return await (p.communicate())[0].decode('code').splitlines()
async def main():
funcs = asyncio.gather(*[run(p) for p in parameters]*)
for f in asyncio.as_completed(funcs):
ret = await f
if sys.platform.startswith('win'):
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
else:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
I feel like I'm writing just something similar. Will I write about openpyxl next?
Recommended Posts