Created
February 14, 2025 19:21
-
-
Save hughdbrown/a69e86a2a45ea414d1bd1f67e3cb055a to your computer and use it in GitHub Desktop.
Show that asyncio can be run on local files
This file contains hidden or 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
#!/usr/bin/env python3 | |
import asyncio | |
from pathlib import Path | |
async def line_count(filename): | |
try: | |
count = filename.read_text(encoding='utf-8').count('\n') | |
except: | |
count = -1 | |
return (filename, count) | |
async def main(): | |
result = await asyncio.gather( | |
*[line_count(p) for p in Path('.').glob('*.txt')] | |
) | |
for r in result: | |
print(f"{str(r[0])} {r[1]}") | |
if __name__ == '__main__': | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this to check async.
pip install aiofiles