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.1.tar.gz (3.7 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.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: single_consumer_queue-1.1.1.tar.gz
  • Upload date:
  • Size: 3.7 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.1.tar.gz
Algorithm Hash digest
SHA256 95d65ed67d27fe93b08be05545932c385aa13241582c8f83c3b1f407894b5f44
MD5 4cdb481928f7c427c0b613cee4be9022
BLAKE2b-256 6cae559a915142f8b682d3fd459989d66e6b8218e1fdbef07691d3f4aa52a9d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for single_consumer_queue-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 911681e4c32ddea9f6328cf31e5a4236b7c72b641d39b5d22f997fcdf4792a3b
MD5 0385c6b46527d9621d4a6121048ef47a
BLAKE2b-256 62913e6f0a9fc6e709b2d26b91c3fd76b12670fea979eed5528919213558160c

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