Skip to main content

Tiny helper for running your async code

Project description

😈 What the heck?

aioGo is the tiny helper library that doing two simple thinks:

  • run your async code

and

  • correct terminate your async code

Default python asyncio have troubles with this: when you hit Ctrl+C asyncio just cancel all your coroutines.

This can lead to many race conditions and hanging your application at exit point.

📘 How to use?

Install from pypi:

pip install aiogo

or, if you prefer poetry:

poetry add aiogo

And just:

from asyncio import Future
from aiogo import go

async def main(termination: Future[None]) -> None:
    await termination

if __name__ == "__main__":
    go(main)

That's all. You just wait while termination future was resolved (when your application got SIGINT or SIGTERM).

When you will be use this library in real world, just:

from asyncio import Future, wait, FIRST_COMPLETED, get_running_loop

async def main(termination: Future[None]) -> None:
    your_awesome_read_task = get_running_loop().create_future()
    done, _ = wait((termination, your_awesome_read_task), return_when=FIRST_COMPLETED)

and check done set.

I want custom event loop (uvloop)!

That I have! Again, just:

from asyncio import Future

import uvloop  # noqa
from aiogo import go

async def main(termination: Future[None]) -> None:
    ...

go(main, event_loop_factory=uvloop.new_event_loop)

I want forced exit!

If you can't write correct exit behaviour, well... Do that:

from asyncio import Future

from aiogo import go

async def main(termination: Future[None]) -> None:
    ...

go(main, exit_timeout=10)

And all your coroutines will be got CancelledError, as doing old good asyncio.run.

I want use threads inside my async application

If you needed, you can make your own pool executor to use it as default executor, like this:

from asyncio import Future
from concurrent.futures import ThreadPoolExecutor

from aiogo import go

async def main(termination: Future[None]) -> None:
    ...


with ThreadPoolExecutor() as pool_executor:
    go(main, default_executor=pool_executor)

Warning! Passed pool will be closed after main routine was has been terminated

I want pass additionally argument to my main coroutine

Just use functools.partial method to bind coroutine to arguments

from asyncio import Future
from functools import partial
from aiogo import go

async def main(terminate: Future[None], value: int) -> None:
    await terminate

go(partial(main, value=42))

BTW any value, that you return from main coroutine will be returned from go function.

🌎 Hot to contribute?

  • Make a repository fork
  • Apply your changes (don't forget install pre-commit before making commit)
  • Make a pull request

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

aiogo-0.2.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

aiogo-0.2.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file aiogo-0.2.0.tar.gz.

File metadata

  • Download URL: aiogo-0.2.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.1 Windows/10

File hashes

Hashes for aiogo-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cf2ff95c8cdf2ef6a59ce59a172d2a07251de82a00316151545f899f5101e0f4
MD5 17facfa7e36ea3e689f3e1a7f1910393
BLAKE2b-256 33ff510268b6920979502c78567574b7b5b5e5b8df096ea595280a17771b0bfc

See more details on using hashes here.

File details

Details for the file aiogo-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: aiogo-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.1 Windows/10

File hashes

Hashes for aiogo-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 135da0ba7619b76a20c64d57c2fdc59e95a74e23b51bc118c17cc32041d65309
MD5 a3a02c7f20e936f83e292ef0a8c6eb55
BLAKE2b-256 69c2594965ddc10d28f25354b07902ad2a0f94cc60acc34a97c4cffe11d96758

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