AsyncIO Throttled Writer
A Python library providing a throttled stream writer for asyncio applications. This library helps prevent network flooding by enforcing minimum intervals between write operations while maintaining the asyncio StreamWriter interface.
Features
- Throttling Control: Set minimum intervals between write operations
- Multiple Write Modes: Support for bytewise, whole message, and non-throttled writes
- AsyncIO Compatible: Drop-in replacement for asyncio.StreamWriter
- Thread-Safe: Uses asyncio locks to ensure safe concurrent access
- Flexible: Configurable throttling intervals and optional drain operations
Installation
pip install asyncio-throttled-writer
Quick Start
import asyncio
from asyncio_throttled_writer import ThrottledStreamWriter
async def example():
# Create a regular asyncio connection
reader, writer = await asyncio.open_connection('example.com', 80)
# Wrap it with ThrottledStreamWriter
throttled_writer = ThrottledStreamWriter(writer)
# Set minimum interval between writes (in milliseconds)
throttled_writer.set_min_send_interval_ms(100) # 100ms between writes
# Write data with throttling
await throttled_writer.write(b'Hello, ')
await throttled_writer.write(b'World!')
# Clean up
throttled_writer.close()
await throttled_writer.wait_closed()
# Run the example
asyncio.run(example())
API Reference
ThrottledStreamWriter
A throttled wrapper around asyncio.StreamWriter that enforces minimum intervals between write operations.
Constructor
ThrottledStreamWriter(writer: StreamWriter)
writer: An asyncio StreamWriter instance to wrap
Methods
set_min_send_interval_ms(ms: float) -> None
Set the minimum interval between write operations.
ms: Minimum interval in milliseconds (negative values are treated as 0)
async write(msg_bytes: bytes, mode: str = "whole", drain: bool = False) -> None
Write bytes with optional throttling.
msg_bytes: The bytes to writemode: Write mode - one of:"whole"(default): Send entire message at once with throttling"bytewise": Send one byte at a time with throttling per byte"no_throttle": Send immediately without throttling
drain: If True, call drain after each write operation
async drain() -> None
Drain the underlying writer buffer.
close() -> None
Close the underlying writer.
async wait_closed() -> None
Wait for the underlying writer to close completely.
Other Methods
The class also provides all standard StreamWriter methods:
can_write_eof()write_eof()writelines(data)get_extra_info(name, default=None)is_closing(property)transport(property)
Usage Examples
Basic Throttling
import asyncio
from asyncio_throttled_writer import ThrottledStreamWriter
async def send_data():
reader, writer = await asyncio.open_connection('localhost', 8080)
throttled = ThrottledStreamWriter(writer)
# Throttle to maximum 10 writes per second
throttled.set_min_send_interval_ms(100)
for i in range(5):
await throttled.write(f"Message {i}\n".encode())
throttled.close()
await throttled.wait_closed()
Bytewise Throttling
async def slow_char_by_char():
reader, writer = await asyncio.open_connection('localhost', 8080)
throttled = ThrottledStreamWriter(writer)
# Very slow: 1 character per second
throttled.set_min_send_interval_ms(1000)
# Send each byte with throttling
await throttled.write(b"Hello World!", mode="bytewise")
throttled.close()
await throttled.wait_closed()
Mixed Throttling and Non-Throttled Writes
async def mixed_writes():
reader, writer = await asyncio.open_connection('localhost', 8080)
throttled = ThrottledStreamWriter(writer)
throttled.set_min_send_interval_ms(500)
# Fast initial handshake
await throttled.write(b"CONNECT\n", mode="no_throttle")
# Throttled data transmission
for i in range(3):
await throttled.write(f"DATA {i}\n".encode(), mode="whole")
# Fast closing
await throttled.write(b"QUIT\n", mode="no_throttle")
throttled.close()
await throttled.wait_closed()
Use Cases
- Rate-Limited APIs: Prevent exceeding API rate limits
- Network Congestion Control: Avoid overwhelming slower network connections
- Protocol Compliance: Meet timing requirements of specific protocols
- Testing: Simulate slow network conditions for testing purposes
- Embedded Systems: Control data flow to resource-constrained devices
Requirements
- Python 3.8+
- asyncio (built-in)
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Metadata
Release files for asyncio-throttled-writer 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asyncio_throttled_writer-0.1.2.tar.gz | 6.5 kB | Without provenance | Details for asyncio_throttled_writer-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| asyncio_throttled_writer-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for asyncio_throttled_writer-0.1.2-py3-none-any.whl |
Total release size: 12.7 kB
Release files / asyncio_throttled_writer-0.1.2.tar.gz
| Download URL | asyncio_throttled_writer-0.1.2.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5494527d8c5043bde37e96fd44566a4e828ac374749f17f89f5bf75c82e01a69
|
|
BLAKE2b-256 checksum How to use checksums |
50398376ac3f2d3222b0d6e62347e3685fba0c1ed52daa2ba4222e8fca2d1139
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / asyncio_throttled_writer-0.1.2-py3-none-any.whl
| Download URL | asyncio_throttled_writer-0.1.2-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
80cd5bc3a96cc248dd4c774c031103c9458df540d91f143f95812e9518df0f8a
|
|
BLAKE2b-256 checksum How to use checksums |
3b3e2fee824a39ad2ef341c9154b58781eb508664ae15c1eff8e57b8d7063165
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for asyncio-throttled-writer 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.