Skip to main content
Coveralls Actions Latest Version https://img.shields.io/pypi/wheel/aiomisc.svg https://img.shields.io/pypi/pyversions/aiomisc.svg https://img.shields.io/pypi/l/aiomisc.svg

Miscellaneous utils for asyncio.

As a programmer, you are no stranger to the challenges that come with building and maintaining software applications. One area that can be particularly difficult is designing the architecture of software that uses asynchronous I/O.

This is where aiomisc comes in. aiomisc is a Python library that provides a collection of utility functions and classes for working with asynchronous I/O in a more intuitive and efficient way. It is built on top of the asyncio library and is designed to make it easier for developers to write asynchronous code that is both reliable and scalable.

With aiomisc, you can take advantage of powerful features like worker pools, connection pools, circuit breaker pattern, and retry mechanisms such as asyncbackoff and asyncretry to make your asyncio code more robust and easier to maintain. In this documentation, we’ll take a closer look at what aiomisc has to offer and how it can help you streamline your asyncio service development.

Why use aiomisc?

Problem: Production asyncio applications require significant boilerplate for logging, graceful shutdown, thread pools, and error handling.

Solution: aiomisc handles infrastructure so you can focus on business logic.

Plain asyncio

With aiomisc

Manual signal handling

Built into entrypoint

Manual logging setup

Single parameter

Manual thread pool

Automatic + @threaded

Try/finally cleanup

Service stop() method

Installation

Installation is possible in standard ways, such as PyPI or installation from a git repository directly.

Installing from PyPI:

pip3 install aiomisc

Installing from github.com:

# Using git tool
pip3 install git+https://github.com/aiokitchen/aiomisc.git

# Alternative way using http
pip3 install \
    https://github.com/aiokitchen/aiomisc/archive/refs/heads/master.zip

The package contains several extras and you can install additional dependencies if you specify them in this way.

With uvloop:

pip3 install "aiomisc[uvloop]"

With aiohttp:

pip3 install "aiomisc[aiohttp]"

Complete table of extras below:

example

description

pip install aiomisc[aiohttp]

For running aiohttp applications.

pip install aiomisc[asgi]

For running ASGI applications

pip install aiomisc[carbon]

Sending metrics to carbon (part of graphite)

pip install aiomisc[cron]

use croniter for scheduling tasks

pip install aiomisc[raven]

Sending exceptions to sentry using raven

pip install aiomisc[rich]

Use rich for logging

pip install aiomisc[uvicorn]

For running ASGI application using uvicorn

pip install aiomisc[uvloop]

use uvloop as a default event loop

You can combine extras values by separating them with commas, for example:

pip3 install "aiomisc[aiohttp,cron,rich,uvloop]"

Quick Start

This section will cover how this library creates and uses the event loop and creates services. Of course, you can’t write about everything here, but you can read about a lot in the Tutorial section, and you can always refer to the Modules and API reference sections for help.

Event-loop and entrypoint

Let’s look at this simple example first:

import asyncio
import logging

import aiomisc

log = logging.getLogger(__name__)

async def main():
    log.info('Starting')
    await asyncio.sleep(3)
    log.info('Exiting')


if __name__ == '__main__':
    with aiomisc.entrypoint(log_level="info", log_format="color") as loop:
        loop.run_until_complete(main())

This code declares an asynchronous main() function that exits after 3 seconds. It would seem nothing interesting, but the whole point is in the entrypoint.

What does the entrypoint do, it would seem not so much, it creates an event-loop and transfers control to the user. However, under the hood, the logger is configured in a separate thread, a pool of threads is created, services are started, but more on that later and there are no services in this example.

Alternatively, you can use the standard asyncio.Runner:

import asyncio

async def main():
    await asyncio.sleep(1)

if __name__ == '__main__':
    with asyncio.Runner() as runner:
        runner.run(main())

Services

The main thing that an entrypoint does is start and gracefully stop services.

The service concept within this library means a class derived from the aiosmic.Service class and implementing the async def start(self) -> None: method and optionally the async def stop(self, exc: Optional[ Exception]) -> None method.

The concept of stopping a service is not necessarily is pressing Ctrl+C keys by user, it’s actually just exiting the entrypoint context manager.

The example below shows what your service might look like:

from aiomisc import entrypoint, Service

class MyService(Service):
    async def start(self):
        do_something_when_start()

    async def stop(self, exc):
        do_graceful_shutdown()


with entrypoint(MyService()) as loop:
    loop.run_forever()

The entry point can start as many instances of the service as it likes, and all of them will start concurrently.

There is also a way if the start method is a payload for a service, and then there is no need to implement the stop method, since the running task with the start function will be canceled at the stop stage. But in this case, you will have to notify the entrypoint that the initialization of the service instance is complete and it can continue.

Like this:

import asyncio
from threading import Event
from aiomisc import entrypoint, Service

event = Event()

class MyService(Service):
    async def start(self):
        # Send signal to entrypoint for continue running
        self.start_event.set()
        await asyncio.sleep(3600)


with entrypoint(MyService()) as loop:
    assert event.is_set()

The whole power of this library is in the set of already implemented or abstract services. Such as: AIOHTTPService, ASGIService, TCPServer, UDPServer, TCPClient, PeriodicService, CronService and so on.

Unfortunately in this section it is not possible to pay more attention to this, please pay attention to the Tutorial section section, there are more examples and explanations, and of course you always can find out an answer on the /api/index or in the source code. The authors have tried to make the source code as clear and simple as possible, so feel free to explore it.

Versioning

This software follows Semantic Versioning

Summary: it’s given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes

  • MINOR version when you add functionality in a backwards compatible manner

  • PATCH version when you make backwards compatible bug fixes

  • Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

In this case, the package version is assigned automatically with poem-plugins, it using on the tag in the repository as a major and minor and the counter, which takes the number of commits between tag to the head of branch.

How to develop?

This project, like most open source projects, is developed by enthusiasts, you can join the development, submit issues, or send your merge requests.

In order to start developing in this repository, you need to do the following things.

Should be installed:

  • Python 3.7+ as python3

  • Installed Poetry as poetry

For setting up developer environment just execute:

# installing all dependencies
poetry install

# setting up pre-commit hooks
poetry run pre-commit install

# adding poem-plugins to the poetry
poetry self add poem-plugins

Release files for aiomisc 18.0.28

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

Source distribution (sdist)

Source distribution for aiomisc 18.0.28
File Size Uploaded
aiomisc-18.0.28.tar.gz 507.4 kB Details

Built distribution (wheel)

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

Total release size: 602.3 kB

Release files / aiomisc-18.0.28.tar.gz

Download URL aiomisc-18.0.28.tar.gz
Size 507.4 kB
Tags Source
SHA-256 checksum
How to use checksums
480fe2243db713acded53bb3c9c0d04dc5b9fe72e83204dd17dab386b0556934
BLAKE2b-256 checksum
How to use checksums
a817b63b8354b6a3c88d5a67db70e5b4ed0a5a37bd24580c5d1ac7faf5b4dbfc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / aiomisc-18.0.28-py3-none-any.whl

Download URL aiomisc-18.0.28-py3-none-any.whl
Size 94.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0db40558fca3c1ee2eaac2235b8a71f3b222144932903bd4197b6e901cb040d4
BLAKE2b-256 checksum
How to use checksums
fa43a455195c7ba20f08effa5d5c53807dd4721734146524df5f26d1dbad1b97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

18.0.28 This release

2 release files

17.9.9

2 release files

17.9.6

2 release files

17.9.1

2 release files

17.9.0

2 release files

17.8.1

2 release files

17.6.3

2 release files

17.6.1

2 release files

17.5.6

2 release files

17.5.4

2 release files

17.5.2

2 release files

17.3.4

2 release files

17.3.0

2 release files

17.2.9

2 release files

17.2.8

2 release files

17.2.6

2 release files

17.2.4

2 release files

17.1.4

2 release files

17.1.3

2 release files

17.1.2

2 release files

17.1.0

2 release files

17.0.8

2 release files

17.0.6

2 release files

17.0.4

2 release files

17.0.3

2 release files

17.0.0

2 release files

16.3.9

2 release files

16.3.7

2 release files

16.3.6

2 release files

16.3.5

2 release files

16.3.2

2 release files

16.3.1

2 release files

16.3.0

2 release files

16.2.9

2 release files

16.2.8

2 release files

16.2.4

2 release files

16.2.3

2 release files

16.2.2

2 release files

16.2.1

2 release files

16.2.0

2 release files

16.1.8

2 release files

16.1.7

2 release files

16.1.5

2 release files

16.1.4

2 release files

16.1.3

2 release files

16.1.2

2 release files

16.1.1

2 release files

16.0.8

2 release files

16.0.4

2 release files

15.9.5

2 release files

15.9.4

2 release files

15.9.3

2 release files

15.7.3

2 release files

15.7.2

2 release files

15.6.8

2 release files

15.6.7

2 release files

15.6.6

2 release files

15.6.5

2 release files

15.6.4

2 release files

15.6.2

2 release files

15.5.0

2 release files

15.4.9

2 release files

15.4.8

2 release files

15.4.7

2 release files

15.4.1

2 release files

15.3.2

2 release files

15.2.4

2 release files

15.2.3

2 release files

15.1.3

2 release files

15.1.2

2 release files

15.1.0

2 release files

15.0.7

2 release files

15.0.6

2 release files

15.0.5

2 release files

15.0.3

2 release files

15.0.2

2 release files

15.0.1

2 release files

14.4.6

2 release files

14.4.3

2 release files

14.4.2

2 release files

14.4.1

2 release files

14.3.2

2 release files

14.2.0

2 release files

14.1.8

2 release files

14.1.0

2 release files

14.0.7

2 release files

14.0.3

2 release files

14.0.0

2 release files

13.0.0

2 release files

12.0.0

2 release files

11.2.0

2 release files

10.2.0

2 release files

10.1.6

2 release files

10.0.8

2 release files

10.0.1

2 release files

10.0.0

2 release files

9.8.4

2 release files

9.8.1

2 release files

9.7.8

2 release files

9.7.7

2 release files

9.7.5

2 release files

9.7.2

2 release files

9.7.1

2 release files

9.7.0

2 release files

9.6.36

2 release files

9.6.11

2 release files

9.6.8

2 release files

9.6.3

2 release files

9.6.0

2 release files

9.4.2

2 release files

9.4.1

2 release files

9.3.0

2 release files

9.2.15

2 release files

9.2.7

2 release files

9.2.6

2 release files

9.2.4

2 release files

9.2.0

2 release files

8.0.3

2 release files

8.0.1

2 release files

8.0.0

2 release files

7.2.5

2 release files

7.2.0

2 release files

7.1.0

2 release files

7.0.3

2 release files

7.0.1

2 release files

6.0.6

2 release files

6.0.4

2 release files

5.5.1

2 release files

5.4.29

2 release files

5.4.26

2 release files

5.4.20

2 release files

5.4.16

2 release files

5.4.14

2 release files

5.4.10

2 release files

5.4.9

2 release files

5.4.8

2 release files

5.4.0

2 release files

5.3.4

2 release files

5.3.2

2 release files

5.3.0

2 release files

5.2.5

2 release files

5.2.3

2 release files

5.2.2

2 release files

5.2.1

2 release files

5.2.0

2 release files

5.1.6

2 release files

5.1.5

2 release files

5.1.4

2 release files

5.1.3

2 release files

5.0.0

2 release files

4.2.16

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.1.7

2 release files

2.1.6

2 release files

2.1.5

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.9.6

2 release files

1.9.3

2 release files

1.9.2

2 release files

1.9.1

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.9

2 release files

1.3.3

2 release files

1.3.0

2 release files

1.1.2

2 release files

1.1.0

2 release files

1.0.7

2 release files

1.0.4

2 release files

1.0.3

2 release files

0.11.9

2 release files

0.11.7

2 release files

0.11.4

2 release files

0.11.0

2 release files

0.10.6

2 release files

0.10.5

2 release files

0.10.4

2 release files

0.10.0

2 release files

0.9.8

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.0

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.0

2 release files

0.6.8

2 release files

0.6.7

2 release files

0.6.1

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.6

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.6

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

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