Asynchronous programming has become increasingly popular in Python. The asyncio module provides infrastructure for writing asynchronous code using the async/await syntax. Meanwhile, queues remain a useful tool for coordinating work between threads or processes. This article explains the key differences between asyncio and queues and when to use each in Python.
Async IO Overview
The
For I/O bound applications like web servers,
Here is some example asyncio code:
import asyncio
async def fetch_data():
print('start fetching')
await asyncio.sleep(2) # simulate waiting for I/O
print('done fetching')
return {'data': 1}
async def print_numbers():
for i in range(10):
print(i)
await asyncio.sleep(0.25)
async def main():
task1 = asyncio.create_task(fetch_data())
task2 = asyncio.create_task(print_numbers())
value = await task1
print(value)
await task2
asyncio.run(main())
This concurrently runs
Queue Overview
A queue data structure allows threads or processes to safely exchange data. One or more producers add data to the queue, and one or more consumers take data out of the queue. The queue handles the synchronization and buffering between them.
The
from threading import Thread
from queue import Queue
def worker(queue):
while True:
item = queue.get()
print(f'Working on {item}')
print(f'Finished {item}')
queue.task_done()
q = Queue()
for i in range(10):
t = Thread(target=worker, args=(q,))
t.daemon = True
t.start()
for item in range(20):
q.put(item)
q.join()
This allows coordinating work between several threads, with the Queue handling the synchronization.
Key Differences
Now that we have looked at their basic usage, what are the main differences between asyncio and queues?
Concurrency Model
The key difference is their concurrency model:
So asyncio enables concurrency through coroutines in a single thread, while queues use separate threads/processes.
Use Cases
Consequently, here are the typical use cases for each:
For I/O bound applications that need scalability, asyncio is usually the best approach in Python. But queues still serve an important role in coordinating CPU bound processes across threads or servers.
Performance
In terms of performance:
So there is a CPU/throughput tradeoff - asyncio provides very lightweight concurrency, while queues enable more parallelism.
Granularity
Finally, the granularity levels differ:
So asyncio allows managing concurrency at a more fine-grained level.
When to Use Each
Based on their differences, here is guidance on when to use asyncio vs queues:
So in summary:
Hopefully this gives you a better sense of how to decide between using asyncio or queues in Python! Each has strengths in particular use cases and concurrency models.