Skip to main content

The simple solution for sharing async data streams in Python.

Project description

streamx

PyPI PyPI - License

The simple solution for sharing async data streams in Python.

Installation

pip install streamx

Usage

Creating a stream

from streamx import AsyncStream

stream = AsyncStream[int]()

Putting items in a stream

You can place items in a stream using the put method. This method is a coroutine, so you'll need to await it. All iterators receive each item placed in the stream.

await stream.put(1)
await stream.put(2)
await stream.put(3)

Consuming a stream

To consume a stream, you can use an async for loop. Many tasks can listen to the stream at the same time, and each task will receive each item put in the stream while it is iterating.

async for item in stream:
    print(item)

Closing a Stream

Once you're done placing data into a stream, you should close it to signal to iterators that there will be no more data. This signals to exit the async for loop.

await stream.close()

Example

import asyncio

from streamx import AsyncStream


async def producer(stream: AsyncStream[int]):
    async with stream:
        for i in range(5):
            await stream.put(i)
            await asyncio.sleep(1)


async def consumer(stream: AsyncStream[int]):
    async for item in stream:
        print(item)


async def main():
    stream = AsyncStream[int]()
    await asyncio.gather(consumer(stream), consumer(stream), producer(stream))


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

streamx-0.0.5.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.

streamx-0.0.5-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file streamx-0.0.5.tar.gz.

File metadata

  • Download URL: streamx-0.0.5.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for streamx-0.0.5.tar.gz
Algorithm Hash digest
SHA256 e849cce01572f1c6d26ca3ad49efb5fcbc6568bc687420f0d1263b5388e0715f
MD5 56988685acd4d143f359fecd7fa32caf
BLAKE2b-256 1815d57b4c371f7189e8eb02b6adc36bca4395daee9bedddaec263416295121c

See more details on using hashes here.

File details

Details for the file streamx-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: streamx-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for streamx-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c6fde6685ab14089f38b60bcb365964ad7b1c95105a2d2e1ae9dc6e23e003344
MD5 e97fd5a1014ac93df6608b9eed3ea199
BLAKE2b-256 2afceab1d5ef69abda71bb36eeb38029f08af21d68315f19a753778024f21e9e

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