Module 05 / Project 03 — Async File Processing¶
Home: README · Module: Async Python
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
aiofilesfor non-blocking file I/O- Async generators with
async for - Processing multiple files concurrently
- Comparing sync vs async file processing
Why this project exists¶
File processing is a common task. When you process many files, async lets you read the next file while the OS is still fetching data for the current one. This project shows how async file I/O works and when it helps.
Run¶
Expected output¶
--- Generating sample files ---
Created 10 sample files in data/
--- Sync file processing ---
Processed file_01.txt: 50 lines, 250 words
Processed file_02.txt: 50 lines, 248 words
...
Sync total: ~X seconds
--- Async file processing ---
Processed file_03.txt: 50 lines, 252 words
Processed file_01.txt: 50 lines, 250 words
...
Async total: ~X seconds
--- Async generator demo ---
Yielded line 1 from file_01.txt
Yielded line 2 from file_01.txt
...
Alter it¶
- Process
.csvfiles instead of.txtfiles. Parse them into rows. - Add a file size filter — skip files under 100 bytes.
- Write the summary results to an output file using
aiofiles.
Break it¶
- Try to process a file that does not exist. What exception do you get?
- Open many files without closing them (remove
async with). What happens? - Use
open()(sync) inside an async function. Does it still work? Is it truly async?
Fix it¶
- Add
try/except FileNotFoundErrorto handle missing files gracefully. - Always use
async withto ensure files are closed properly. - Add a semaphore to limit how many files are open at once.
Explain it¶
- When does async file I/O help vs regular file I/O?
- What is an async generator and how does
async forwork? - Why does
async with aiofiles.open()matter compared to regularopen()? - When would you use threads instead of async for file processing?
Mastery check¶
You can move on when you can: - use aiofiles to read/write files asynchronously, - write an async generator that yields lines from a file, - process multiple files concurrently with gather(), - explain when async file I/O is beneficial.