Skip to main content

Multi consumers asyncio compatible queue

Project description

Coverage CI

This library enables concurrent iteration over the same stream of values asynchronously. This can be achieved by either creating a Queue that can be iterated simultanously from multiple consumers or wrapping an async generator to make it also iterable simultaously.

Below is an example of MultiQueue use.

import asyncio
from aio_broadcast import MultiQueue


async def print_stream(stream, name, delay):
    async for i in stream:
        await asyncio.sleep(delay)
        print(name, i)


async def main():
    stream = MultiQueue()
    consumers = [asyncio.create_task(print_stream(stream, f"consumer {i}", 1 / (i + 1))) for i in range(5)]
    for i in range(5):
        await asyncio.sleep(0.2)
        stream.put(i)
    stream.close()
    await asyncio.gather(*consumers)


asyncio.run(main())

An async generator can be iterated through simultanously as follows.

import asyncio
from aio_broadcast import broadcast
import random

random.seed(0)


async def consume_stream(stream, name):
    async for value in stream:
        await asyncio.sleep(random.uniform(0, 0.1))
        print(name, value)


async def stream(values):
    for value in values:
        await asyncio.sleep(random.uniform(0, 0.5))
        yield value


async def main():
    values = list(range(5))
    source_stream = broadcast(stream(values))
    await asyncio.gather(
        *[consume_stream(source_stream) for _ in range(3)],
    )


asyncio.run(main())

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

aio_broadcast-1.0.3.tar.gz (31.8 kB view details)

Uploaded Source

Built Distribution

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

aio_broadcast-1.0.3-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

Details for the file aio_broadcast-1.0.3.tar.gz.

File metadata

  • Download URL: aio_broadcast-1.0.3.tar.gz
  • Upload date:
  • Size: 31.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for aio_broadcast-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ce61b913f23f8a788678ef23570b565973899300fadb3c4585719b9a84bd9f3b
MD5 810ab95790fe601110d019f8efb1ef1a
BLAKE2b-256 2cc0ca89a231ccbe3189d742b2827a29be7bf8260e79367fbab5da2f3a30fe66

See more details on using hashes here.

Provenance

The following attestation bundles were made for aio_broadcast-1.0.3.tar.gz:

Publisher: ci.yml on pytek-io/aio-broadcast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aio_broadcast-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: aio_broadcast-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for aio_broadcast-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3286d4ea944202cb7a23f8319c5e22820c8581315c5e1b767f884354e00e2974
MD5 362f0f9433968f9c2c5637d5f6bde8e5
BLAKE2b-256 7561ed5286564d5ab3790b216ae57027c4c7bd87d3c7db0c24aa27514fb2aa9b

See more details on using hashes here.

Provenance

The following attestation bundles were made for aio_broadcast-1.0.3-py3-none-any.whl:

Publisher: ci.yml on pytek-io/aio-broadcast

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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