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.0.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.0.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: single_consumer_queue-1.0.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.0.0.tar.gz
Algorithm Hash digest
SHA256 d7a03c68571b625c8133bed75badef0f603eb3a8bdd27aa59ac802460a199e82
MD5 dc975658ae0d12fd39110845ced37a5d
BLAKE2b-256 af3027ba3decbfd2ac0ca1f6369b09acf83b141b6162607f8c9ae6cbafd01287

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for single_consumer_queue-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4580a95727bac1c81beb166f970e7bbba40f30dad79bbaa11044f18432175ed8
MD5 7cb6c10939d174810bdf78d8abeda168
BLAKE2b-256 268d6c610c8bd2a1a0180b66a82cee4970f7f03a127d15f5647898bc2b988868

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