mode-streaming

0.4.1 · active · verified Tue Apr 14

Mode-streaming is a Python library built on top of AsyncIO, designed to simplify asynchronous service-based programming. It allows users to define, start, stop, restart, and supervise services, forming a graph of dependencies. This project is a community-maintained fork of the original 'Mode' project, with a focus on continuous releases, code quality, and support for modern Python versions. The current version is 0.4.1.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a basic `Service` with a periodic background task and run it using `mode.Worker`. A service can define `on_start`, `on_stop`, and timed tasks using the `@Service.timer` decorator. For command-line execution, `Worker(Service(), loglevel='info').execute_from_commandline()` is typically used.

import asyncio
from mode import Service, Worker

class MyService(Service):
    async def on_start(self) -> None:
        print("MyService started!")
        self.add_background_task(self.my_periodic_task)

    @Service.timer(1.0)
    async def my_periodic_task(self) -> None:
        print("Periodic task running...")

    async def on_stop(self) -> None:
        print("MyService stopped.")

async def main():
    service = MyService()
    worker = Worker(service, loglevel="info")
    # In a real application, you'd execute from command line
    # For quickstart, manually manage lifecycle (or use execute_from_commandline)
    try:
        await worker.start()
        await asyncio.sleep(3)
    finally:
        await worker.stop()

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →