Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

When should you choose multiprocessing over asyncio, and vice versa? multiprocessing कब चुनें asyncio की जगह, और कब उल्टा?

Answer
ScenarioBest choiceReason
Heavy computation (image processing, math)multiprocessingTrue parallelism across CPU cores, bypasses GIL
Many concurrent network requestsasyncioHandles thousands of connections with low overhead, single thread
Mix of blocking I/O libraries (no async support)threadingSimpler than asyncio when libraries aren't async-aware
CPU-bound + need simplicitymultiprocessing.PoolHigh-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कारण
भारी computationmultiprocessingTrue parallelism, GIL bypass
बहुत सारे concurrent network requestsasyncioहज़ारों connections कम overhead में
Blocking I/O libraries (async support नहीं)threadingasyncio से सरल
# 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 -> threading

Was this answer clear?