asyncio என்பது Python இன் async/await ஐ பயன்படுத்தி concurrent I/O க்கான கட்டமைப்பு. இது event loop வழியாக ஒற்றை thread இல் பல I/O செயல்பாடுகளை ஒத்துழைக்கும் முறையில் இயக்குகிறது — உচ்च concurrency I/O-நோக்கிய வேலைக்கு (நெட்வொர்க் அழைப்புகள், வெப்ப சேவையகங்கள்) நெகிழ்வாக உள்ளது threads இன் overheads இல்லாமல்.
Coroutines ஐ வரையறுத்தல் மற்றும் இயக்குதல்
import asyncio
async def fetch_data(id): # an async function = a coroutine
print(f"start {id}")
await asyncio.sleep(1) # await a non-blocking I/O operation (yields control)
print(f"done {id}")
return id
asyncio.run(fetch_data(1)) # run a coroutine from sync code
ஒரு async def செயல்பாடு ஒரு coroutine ஆகும்; await ஐ I/O புள்ளியில் இடைநிறுத்துகிறது, event loop மற்ற coroutines ஐ இயக்க அனுமதிக்கிறது — பிறகு பெறப்பட்ட செயல்பாடு முடிந்தபோது மீண்டும் தொடங்குகிறது.
முக்கிய நன்மை: concurrent I/O
# ❌ sequential — total time ≈ 3 seconds
async def slow():
await fetch_data(1) # wait 1s
await fetch_data(2) # then wait 1s
await fetch_data(3) # then wait 1s
# ✅ concurrent — total time ≈ 1 second (all run together)
async def fast():
await asyncio.gather(fetch_data(1), fetch_data(2), fetch_data(3))
asyncio.gather coroutines ஐ concurrently இயக்குகிறது — ஒன்று I/O க்கு காத்திருக்கும் போது மற்றவை முன்னேறுகின்றன. மூன்று 1-விநாடி waits ~1 விநாடி மொத்தமாக ஒன்றுசேர்ந்துள்ளன. இதுவே முக்கிய மதிப்பு: ஒற்றை thread இல் பல I/O waits ஐ ஒன்றுசேர்ப்பது.
Threads இலிருந்து இது எப்படி வேறுபடுகிறது
Threading: OS-managed, preemptive, real threads (GIL-limited for CPU)
asyncio: single thread, COOPERATIVE — coroutines yield at `await` points
→ lighter weight (handle thousands of connections), no thread overhead,
but one CPU-heavy coroutine blocks everything (no preemption)
முதல் விதி: event loop ஐ தடுக்க வேண்டாம்
async def bad():
time.sleep(5) # ❌ BLOCKS the whole event loop (sync sleep)
result = heavy_compute() # ❌ CPU work freezes all coroutines
async def good():
await asyncio.sleep(5) # ✅ non-blocking
# offload CPU work to a thread/process pool
await loop.run_in_executor(None, heavy_compute)
இது ஒத்துழைக்கும் மற்றும் single-threaded இருப்பதால், எந்த blocking/CPU-கனமான அழைப்பும் அனைத்து coroutines ஐ பிறழ்த்துகிறது. await-able async APIs ஐ பயன்படுத்துங்கள், மற்றும் blocking/CPU வேலையை executor க்கு மாற்றுங்கள்.
Asyncio சிறப்பாக செயல்படும் இடங்கள்
✓ High-concurrency I/O: web servers (FastAPI), web scraping, many API/DB calls,
websockets, real-time apps — thousands of concurrent connections efficiently
✗ CPU-bound work — use multiprocessing instead (asyncio doesn't help)
இது ஏன் முக்கியம்
asyncio என்பது Python இல் உচ்च-concurrency I/O க்கான நவீன மணிமار, FastAPI போன்ற கட்டமைப்புகளை சக்திவாய்ந்தாக உள்ளது மற்றும் ஒற்றை thread இல் ஆயிரக்கணக்கான ஒரே சமயத்தில் இணைப்புகளை திறமையாக கையாளுவதை செயல்படுத்துகிறது — thread-per-connection ஐ விட மிகவும் இலகுவாக உள்ளது.
Coroutines, await, gather உடன் concurrent செயல்பாடு, மற்றும் குறிப்பாக முதல் விதி (event loop ஐ தடுக்க வேண்டாம்; CPU/blocking வேலையை மாற்று) ஐ புரிந்துகொள்வது performant async Python எழுதுவதற்கு அপरिहार்यம் ஆகும்.
இது எப்போது பொருத்தமாக உள்ளது (I/O-நோக்கிய concurrency) எப்போது இல்லை (CPU-நோக்கிய → multiprocessing) என்பதை அறிந்துகொள்வது அதை திறமையாக பயன்படுத்துவதற்கான தேவையாகும்.
