Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Created February 14, 2025 19:21
Show Gist options
  • Save hughdbrown/a69e86a2a45ea414d1bd1f67e3cb055a to your computer and use it in GitHub Desktop.
Save hughdbrown/a69e86a2a45ea414d1bd1f67e3cb055a to your computer and use it in GitHub Desktop.
Show that asyncio can be run on local files
#!/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())
@hughdbrown
Copy link
Author

But not actually run asynchronously, hahaha.

The file I/O is blocking, so it is not running asynchronously, notwithstanding that asyncio is running the code.

@zephyranthes03
Copy link

But not actually run asynchronously, hahaha.

The file I/O is blocking, so it is not running asynchronously, notwithstanding that asyncio is running the code.

Try this to check async.

pip install aiofiles

#!/usr/bin/env python3

import asyncio
from pathlib import Path
import aiofiles

async def line_count(filename: Path):
    count = 0
    try:
        async with aiofiles.open(filename, mode='r', encoding='utf-8') as f:
            async for _ in f:
                count += 1
    except Exception:
        count = -1
    return (filename, count)

async def main():
    txt_files = list(Path('.').glob('*.txt'))
    result = await asyncio.gather(*(line_count(f) for f in txt_files))
    for file, count in result:
        print(f"{file} {count}")

if __name__ == '__main__':
    asyncio.run(main())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment