Skip to main content

Async utilities library inspired by Tokio

Project description

Kioto

GitHub Actions Workflow Status Codecov PyPI - Version

Kioto is an asynchronous utilities library for Python, inspired by Tokio from Rust. Leveraging Python's asyncio, Kioto provides a suite of powerful async utilities and data structures, enabling developers to build efficient and scalable asynchronous applications with ease.

Features

  • Async Channels: Facilitate communication between asynchronous tasks with bounded and unbounded channels.
  • Futures and Task Management: Simplify asynchronous task handling with utilities for managing task sets, selection, and shared futures.
  • Streams and Sinks: Provide stream processing capabilities, including mapping, filtering, buffering, and more.
  • Synchronization Primitives: Offer advanced synchronization tools like mutexes for managing shared state.
  • Time Utilities: Handle asynchronous timing operations, intervals, and timeouts seamlessly.

Usage Examples

Here are some example programs demonstrating how to use Kioto's features:

Async Channels

Kioto provides channnels with a sender/receiver pair. If one end of the channel isgc'd the other end will raise an exception on send/recv.

import asyncio
from kioto.channels import channel

async def producer(sender):
    for i in range(5):
        await sender.send_async(i)
        print(f"Sent: {i}")

async def consumer(receiver):
    # recv will raise an exception, once producer loop
    # finishes and the sender goes out of scope.
    while item := await receiver.recv()
        print(f"Received: {item}")

def pipeline():
    sender, receiver = channel(10)
    return asyncio.gather(producer(sender), consumer(receiver))

async def main():
    await pipeline()

Select on Task Completion

Use Kioto's task management utilities to await the completion of multiple asynchronous tasks and handle their results using a match statement.

import asyncio
from kioto.futures import task_set, select

async def fetch_data():
    await asyncio.sleep(1)
    return "Data fetched"

async def process_data():
    await asyncio.sleep(2)
    return "Data processed"

async def main():
    tasks = task_set(fetch=fetch_data(), process=process_data())
    while tasks:
        match await select(tasks):
            case "fetch", result:
                print(f"fetched: {result}")
                # Dispatch or handle the fetched data
            case "process", result:
                print(f"processed: {result}")
                # Dispatch or handle the processed data

Mutex with owned contents

Kioto includes syncronization primitives that own their contents.

import asyncio
from kioto.futures import try_join
from kioto.sync import Mutex

class Counter:
    def __init__(self):
        self.value = 0

async def increment(mutex: Mutex, times: int):
	# The guard is only valid in the context manager.
	# It will raise an exception if a reference outlives this scope
    async with mutex.lock() as guard:
        for _ in range(times):
            guard.value += 1
            await asyncio.sleep(0.1)

async def main():
    mutex = Mutex(Counter)
    await try_join(
        increment(mutex, 5),
        increment(mutex, 5)
    )
    
    print(f"Final counter value: {counter.value}")

Stream Combinators

Use stream combinators to implement complex data pipelines.

import asyncio
from kioto import streams

async def main():
    stream = (
        streams.iter(range(10))
            .filter(lambda x: x % 2 == 0)
            .map(lambda x: x * 2)
    )

    # Iterate the stream.
    async for item in stream:
        print(item)

    # Alternatively collect into a list
    values = await stream.collect()

Implement the Stream class by decorating your async generators

import asyncio
from kioto import streams

@streams.async_stream
async def sock_stream(sock):
    while result := await sock.recv(1024):
        yield result

# Read urls off the socket and download them 10 at a time
downloads = await (
    sock_stream(socket)
      .map(lambda url: request.get(url))
      .buffered_unordered(10)
      .collect()
)

License

Kioto is released under the MIT License


Feel free to contribute to Kioto by submitting issues or pull requests on GitHub. For more detailed documentation, visit the official documentation site.

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

kioto-0.1.5.tar.gz (36.3 kB view details)

Uploaded Source

Built Distribution

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

kioto-0.1.5-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file kioto-0.1.5.tar.gz.

File metadata

  • Download URL: kioto-0.1.5.tar.gz
  • Upload date:
  • Size: 36.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.5

File hashes

Hashes for kioto-0.1.5.tar.gz
Algorithm Hash digest
SHA256 b7e6c46ed28e1684f82bdcbe2c7fe7f844b00e53744bc6707b3bd61bc206b75e
MD5 5d5ab8b2a1259bcf3c285163d6acf18e
BLAKE2b-256 e64d6d3815bd03a89792a4b0f42bf909887fa7e87840b77d9b93395b3a60dc35

See more details on using hashes here.

File details

Details for the file kioto-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: kioto-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.5

File hashes

Hashes for kioto-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7bc5af332d5a22fe45031fa7ebbe1e8acbf90e3e4603881afdc868ee4a6b5a89
MD5 134e56085b68387afd2e7f3b6a32f010
BLAKE2b-256 b1f5eab1fa57b2a564bf5c7f546323fde6cb142dbbf16789c0552f9e401bbc6a

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