Interview question
When should you choose multiprocessing over asyncio, and vice versa? multiprocessing कब चुनें asyncio की जगह, और कब उल्टा?
Answer
| Scenario | Best choice | Reason |
|---|---|---|
| Heavy computation (image processing, math) | multiprocessing | True parallelism across CPU cores, bypasses GIL |
| Many concurrent network requests | asyncio | Handles thousands of connections with low overhead, single thread |
| Mix of blocking I/O libraries (no async support) | threading | Simpler than asyncio when libraries aren't async-aware |
| CPU-bound + need simplicity | multiprocessing.Pool | High-level API for distributing work across processes |
# CPU-bound: multiprocessing wins
from multiprocessing import Pool
def square(n):
return n * n
if __name__ == '__main__':
with Pool(4) as pool: # 4 worker processes
results = pool.map(square, range(1000000))
# Genuinely parallel across 4 CPU cores
# I/O-bound with MANY connections: asyncio wins
import asyncio
import aiohttp # async-aware HTTP library
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def fetch_all(urls):
async with aiohttp.ClientSession() as session:
return await asyncio.gather(*[fetch(session, url) for url in urls])
# Can handle THOUSANDS of concurrent requests with minimal memory overhead,
# compared to thousands of OS threads which would be very resource-heavy
# Rule of thumb:
# - CPU-bound -> multiprocessing
# - I/O-bound, need scale (1000s of connections) -> asyncio
# - I/O-bound, simpler code, moderate scale, blocking libraries -> threading| Scenario | बेहतर choice | कारण |
|---|---|---|
| भारी computation | multiprocessing | True parallelism, GIL bypass |
| बहुत सारे concurrent network requests | asyncio | हज़ारों connections कम overhead में |
| Blocking I/O libraries (async support नहीं) | threading | asyncio से सरल |
# CPU-bound: multiprocessing जीतता है
from multiprocessing import Pool
def square(n):
return n * n
if __name__ == '__main__':
with Pool(4) as pool:
results = pool.map(square, range(1000000))
# I/O-bound बहुत सारे connections: asyncio जीतता है
import asyncio
import aiohttp
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
async def fetch_all(urls):
async with aiohttp.ClientSession() as session:
return await asyncio.gather(*[fetch(session, url) for url in urls])
# General rule:
# - CPU-bound -> multiprocessing
# - I/O-bound, बड़ा scale -> asyncio
# - I/O-bound, simple, blocking libraries -> threadingWas this answer clear?