Created
January 22, 2020 20:46
-
-
Save canimus/36e4aa9128803508f66766b2545b4b80 to your computer and use it in GitHub Desktop.
Asyncio Example with Function and Subprocess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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