Skip to content

Instantly share code, notes, and snippets.

@canimus
Created January 22, 2020 20:46
Show Gist options
  • Save canimus/36e4aa9128803508f66766b2545b4b80 to your computer and use it in GitHub Desktop.
Save canimus/36e4aa9128803508f66766b2545b4b80 to your computer and use it in GitHub Desktop.
Asyncio Example with Function and Subprocess
import asyncio
import glob
import aiofiles
import subprocess
async def read_file(f):
print("start: " + f)
async with aiofiles.open(f, 'r') as fd:
lines = await fd.read()
return lines
async def print_file(f):
args = ["echo", f'"{f}"']
process = await asyncio.create_subprocess_exec(
*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
# Status
print("Started: %s, pid=%s" % (args, process.pid), flush=True)
# Wait for the subprocess to finish
stdout, stderr = await process.communicate()
# Progress
if process.returncode == 0:
print(
"Done: %s, pid=%s, result: %s"
% (args, process.pid, stdout.decode().strip()),
flush=True,
)
else:
print(
"Failed: %s, pid=%s, result: %s"
% (args, process.pid, stderr.decode().strip()),
flush=True,
)
# Result
result = stdout.decode().strip()
# Return stdout
return result
async def main():
for x in await asyncio.gather(*list(map(read_file, glob.glob('sql/*.sql')))):
await print_file(x)
if __name__ == '__main__':
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment