Junior — Middle
What methods can be used to implement parallel execution of tasks limited by I/O operations in Python?
sobes.tech AI
Answer from AI
For parallel execution of tasks limited by input-output operations (I/O-bound), in Python, the following approaches can be used:
asynciomodule — asynchronous programming using coroutines. It allows efficient handling of many I/O operations without creating a large number of threads.
import asyncio
async def fetch_data():
await asyncio.sleep(1) # simulate I/O operation
return 'data'
async def main():
results = await asyncio.gather(fetch_data(), fetch_data())
print(results)
asyncio.run(main())
- Threads (
threading) — for I/O tasks, threads work efficiently because GIL does not block input-output operations.
import threading
def io_task():
import time
time.sleep(1) # simulate I/O
print('Task done')
threads = [threading.Thread(target=io_task) for _ in range(5)]
for t in threads:
t.start()
for t in threads:
t.join()
concurrent.futures.ThreadPoolExecutormodule — a convenient way to manage a thread pool.
from concurrent.futures import ThreadPoolExecutor
import time
def io_task():
time.sleep(1)
return 'done'
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(io_task) for _ in range(5)]
for future in futures:
print(future.result())
The choice depends on the specific task and application architecture, but for scalable network applications, asyncio is often preferred.