Skip to main content

Context manager that provides simple graceful shutdown interface for your asyncio tasks.

Project description

aioshutdown

Context manager that provides simple graceful shutdown interface for your asyncio tasks.

Installation

pip install -U aioshutdown

Usage

Just specify the list of signals that you want to intercept using the | operator:

from aioshutdown import SIGTERM, SIGINT, SIGHUP


with SIGTERM | SIGHUP | SIGINT as loop:
    ...

The context manager will return an instance of the current event loop. You can intercept signals directly inside your coroutines:

async def my_task(sleep: int):
    try:
        """ Main logic of your coroutine. """
    except asyncio.CancelledError as exc:
        """ In this place you can gracefully complete the work. """

Full example:

import asyncio
from aioshutdown import SIGTERM, SIGINT, SIGHUP
import logging


logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s,%(msecs)d %(levelname)s: %(message)s",
    datefmt="%H:%M:%S",
)

async def my_task(sleep: int):
    try:
        while True:
            logging.info("Sleep from task #%s", sleep)
            await asyncio.sleep(sleep)
    except asyncio.CancelledError as exc:
        # Received an exit signal. In this place we gracefully complete the work.
        signal = exc.args[0] # NOTE: works in Python >= 3.9 only: https://docs.python.org/3/library/asyncio-future.html?highlight=Changed%20in%20version%203.9:%20Added%20the%20msg%20parameter.#asyncio.Future.cancel
        logging.warning("Received %s signal. Shutting down...", signal.value)

# Usage with `run_forever`

with SIGTERM | SIGHUP | SIGINT as loop:
    tasks = [loop.create_task(my_task(i)) for i in range(1, 10)]
    loop.run_forever()

# Usage with `run_until_complete`

with SIGTERM | SIGHUP | SIGINT as loop:
    tasks = [loop.create_task(my_task(i)) for i in range(1, 10)]
    loop.run_until_complete(asyncio.gather(*results))

Output

23:04:05,798 INFO: Sleep from task #1
23:04:05,798 INFO: Sleep from task #2
23:04:06,799 INFO: Sleep from task #1
23:04:07,800 INFO: Sleep from task #2
23:04:07,800 INFO: Sleep from task #1
23:04:08,801 INFO: Sleep from task #1
^C23:04:09,504 INFO: Received exit signal SIGINT...
23:04:09,504 INFO: Cancelling 2 outstanding tasks
23:04:09,504 WARNING: Received 2 signal. Shutting down...
23:04:09,504 WARNING: Received 2 signal. Shutting down...
23:04:09,504 INFO: Stopping event loop

Useful links

Graceful Shutdowns with asyncio

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

aioshutdown-0.0.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

aioshutdown-0.0.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file aioshutdown-0.0.2.tar.gz.

File metadata

  • Download URL: aioshutdown-0.0.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for aioshutdown-0.0.2.tar.gz
Algorithm Hash digest
SHA256 2a69baf10f4235f51eb3228868406abe39b345081c5c1930e8ce17d4e5a7597d
MD5 1ad54ecde2757c3464a56bfac8c5dff0
BLAKE2b-256 c985b6cd93a9d60b02547124a93090f5163bde9bdc6b1b56e58c218af892a62f

See more details on using hashes here.

File details

Details for the file aioshutdown-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: aioshutdown-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for aioshutdown-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ed75f50ff5a670a4c5cb12b1ecfe5ceb5298e8a2f13de32d52c92d61f4c07a3f
MD5 b531affc22b2ff8265d676e0dd780763
BLAKE2b-256 015a7a89cb1a994e962be9d88567e81d92b34d8ce7d227709e1290bd36badf7e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page