Skip to content

Instantly share code, notes, and snippets.

@bhpayne
Created September 11, 2020 19:00
Show Gist options
  • Save bhpayne/10ad39b3952d4fee3a911387fc44ac55 to your computer and use it in GitHub Desktop.
Save bhpayne/10ad39b3952d4fee3a911387fc44ac55 to your computer and use it in GitHub Desktop.
$ time python loop_with_delay.py
index = 0
index = 1
index = 2

real    0m6.202s
user    0m0.111s
sys     0m0.039s

$ time python loop_with_async.py
index= 2
index= 1
index= 0
['', '', '']

real    0m2.500s
user    0m0.339s
sys     0m0.071s
#!/usr/bin/env python3
import asyncio
async def run_command(*args):
"""
https://asyncio.readthedocs.io/en/latest/subprocess.html
"""
# Create subprocess
process = await asyncio.create_subprocess_exec(
*args,
# stdout must a pipe to be accessible as process.stdout
stdout=asyncio.subprocess.PIPE)
# Wait for the subprocess to finish
stdout, stderr = await process.communicate()
# Return stdout
return stdout.decode().strip()
async def go_do_something(index: int) -> None:
print('index=',index)
res = await run_command('sleep','2')
return res
def my_long_func(val: int) -> None:
task_list = []
for indx in range(val):
task_list.append( go_do_something(indx) )
loop = asyncio.get_event_loop()
commands = asyncio.gather(*task_list)
reslt = loop.run_until_complete(commands)
print(reslt)
loop.close()
my_long_func(3) # launch three tasks
#!/usr/bin/env python3
# https://docs.python.org/3/library/subprocess.html
import subprocess
from subprocess import PIPE
def go_do_something(index: int) -> None:
"""
This function takes a long time
Nothing is returned
Each instance is independent
"""
process = subprocess.run(["sleep","2"],stdout=PIPE,stderr=PIPE,timeout=20)
stdout = process.stdout.decode("utf-8")
stderr = process.stderr.decode("utf-8")
if "error" in stderr:
print("error for "+str(index))
return
def my_long_func(val: int) -> None:
"""
This function contains a loop
Each iteration of the loop calls a function
Nothing is returned
"""
for index in range(val):
print("index = "+str(index))
go_do_something(index)
my_long_func(3) # launch three tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment