Skip to main content

Asyncio high-performance single consumer queue

Project description

Single Consumer Queue

Single Consumer Queue is a Python library that provides an alternative to the standard asyncio.Queue for single consumer scenarios. It consists of two classes: SingleConsumerQueue and SingleConsumerPriorityQueue. Both classes implement the AbstractSingleConsumerQueue abstract base class, which provides the basic functionality of a single consumer queue.

Why Single Consumer Queue?

In some scenarios, the standard asyncio.Queue can be slower than necessary. This is because asyncio.Queue is designed to be used with multiple consumers, which means that it has additional overhead to handle multiple concurrent accesses. If you only have one consumer, you can use SingleConsumerQueue or SingleConsumerPriorityQueue to reduce this overhead and improve performance.

How to use Single Consumer Queue

Installation You can install Single Consumer Queue using pip:

pip install single-consumer-queue

Usage

Here's an example of how to use SingleConsumerQueue:

async def consumer(queue: SingleConsumerQueue | SingleConsumerPriorityQueue):
    async for item in queue.start_consuming():
        print(item)

SingleConsumerQueue raises exception if you try to add multiple consumers:

async def consumer(queue: SingleConsumerQueue | SingleConsumerPriorityQueue):
    async for item in queue.start_consuming():
        print(item)

queue = SingleConsumerQueue()
asyncio.create_task(consumer(queue))
await asyncio sleep(0.1)
await queue.get()  # raises runtime error

Lock is checked and acquired when consumer starts and every time that get is awaited.

Get has considerate overhead because it has to use lock every time, so it is recommended to use start_consuming generator when you want to consume items in the loop.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

single_consumer_queue-1.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

single_consumer_queue-1.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file single_consumer_queue-1.1.0.tar.gz.

File metadata

  • Download URL: single_consumer_queue-1.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Darwin/22.4.0

File hashes

Hashes for single_consumer_queue-1.1.0.tar.gz
Algorithm Hash digest
SHA256 4a4ba577d23272c66d73b62bedc903bbc0b9207dad9f928a59750e86420e56db
MD5 6ce73f8887697a1a72010a32b3a8147b
BLAKE2b-256 d8b43bb8bf108b7ca9ac7a9fc4668bfa701b97de70c774eb4257010a90ef04d2

See more details on using hashes here.

File details

Details for the file single_consumer_queue-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for single_consumer_queue-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cec0920dbcba8b4538c015eca966311a8620dbc64570a4bb1e27ce7e241f615a
MD5 653a958287ae626b49b0e1f07097f56b
BLAKE2b-256 57a9bdb00509e0adbfa2f7886e972635d548a3de674a6cae451d90c7a3c81aa3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page