Skip to main content

Add your description here

Project description

amgi-common

This package includes some useful helpers for writing AMGI servers.

Installation

pip install amgi-common==0.15.1

Constructs

Lifespan

This class handles the AMGI lifespan protocol.

from amgi_common import Lifespan


async def main_loop(app, state):
    """Handle event batches"""


async def serve(app):
    async with Lifespan(app) as state:
        # handle app calls
        await main_loop(app, state)

Stoppable

This class should help with graceful shutdowns. Whenever you have a call to something with a timeout, it cancels that task. . For example, if you have a client that has a method fetch_messages, which has a timeout you could loop like so:

from amgi_common import Stoppable


class Server:
    def __init__(self, app):
        self._app = app
        self._stoppable = Stoppable()
        self._client = Client()
        self._running = True

    async def main_loop(self, app, state):
        while self._running:
            messages = await self._client.fetch_messages(timeout=10_000)
            # Handle messages

    def stop(self):
        self._running = False

There are several ways to deal with this:

  1. The above example, where on stop you will have to wait for the timeout plus the time to handle messages
  2. Run the main loop in a task, and cancel it on stop

Both have their own problems. In the first case, you could be waiting a long time. In the second case, when you receive messages, you should probably process them before shutting down.

The class Stoppable is there to help you. It will return the results of the callable iterably, but if the stoppable has been told to stop, it will cancel any tasks it has running in the background, and stop any current iterations.

from amgi_common import Stoppable


class Server:
    def __init__(self, app):
        self._app = app
        self._stoppable = Stoppable()
        self._client = Client()

    async def main_loop(self, app, state):
        async for messages in self._stoppable.call(
            self._client.fetch_messages, timeout=10_000
        ):
            # Handle messages
            pass

    def stop(self):
        self._stoppable.stop()

Contact

For questions or suggestions, please contact jack.burridge@mail.com.

License

Copyright 2025 AMGI

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

amgi_common-0.15.1.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

amgi_common-0.15.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file amgi_common-0.15.1.tar.gz.

File metadata

  • Download URL: amgi_common-0.15.1.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.6

File hashes

Hashes for amgi_common-0.15.1.tar.gz
Algorithm Hash digest
SHA256 a6964c66f0094ccf0d8f0fc63b8a51779f1ae6c5cc428e596dff5d954834d145
MD5 536eead11499c867762fead92b1e3d73
BLAKE2b-256 28f716254c620fdb9b781c7967050f5b96699302ef53190ac2daade1f1e4507c

See more details on using hashes here.

File details

Details for the file amgi_common-0.15.1-py3-none-any.whl.

File metadata

File hashes

Hashes for amgi_common-0.15.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7a9d326b2b0329b5cae55864560947f36ec01475df853e8bbc63452dde40852e
MD5 4a040bb50ee1eef42ec4ce015ffb7d3f
BLAKE2b-256 1e210da9a8ff0c6e92999ac7029f949f744ae6a28f1f861141898948cb08d05d

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