Junior — Middle
What methods in Python are used to implement parallel operations when processing requests?
sobes.tech AI
Answer from AI
In Python, several approaches are often used to implement parallel operations when processing requests:
- Multithreading — suitable for tasks with a large number of I/O operations, as Python is limited by the GIL (Global Interpreter Lock) and cannot efficiently utilize multiple cores for CPU-intensive tasks.
import threading
def handle_request(request):
# handle request
pass
thread = threading.Thread(target=handle_request, args=(request,))
thread.start()
- Multiprocessing — allows running multiple processes, bypassing the GIL limitation, effective for CPU-intensive tasks.
from multiprocessing import Pool
def process_request(request):
# handle request
return result
with Pool(4) as p:
results = p.map(process_request, requests)
- Asynchronous programming (asyncio) — enables processing many requests without creating new threads or processes, effective for a large number of I/O operations.
import asyncio
async def handle_request(request):
# asynchronous handling
pass
async def main():
tasks = [handle_request(req) for req in requests]
await asyncio.gather(*tasks)
asyncio.run(main())
The choice of method depends on the nature of the task and performance requirements.