Skip to main content

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())

Metadata

Release files for streamx 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamx 0.0.5
File Size Uploaded
streamx-0.0.5.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for streamx 0.0.5
File Interpreter ABI Platform
streamx-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 7.8 kB

Release files / streamx-0.0.5.tar.gz

Download URL streamx-0.0.5.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e849cce01572f1c6d26ca3ad49efb5fcbc6568bc687420f0d1263b5388e0715f
BLAKE2b-256 checksum
How to use checksums
1815d57b4c371f7189e8eb02b6adc36bca4395daee9bedddaec263416295121c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release files / streamx-0.0.5-py3-none-any.whl

Download URL streamx-0.0.5-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c6fde6685ab14089f38b60bcb365964ad7b1c95105a2d2e1ae9dc6e23e003344
BLAKE2b-256 checksum
How to use checksums
2afceab1d5ef69abda71bb36eeb38029f08af21d68315f19a753778024f21e9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page