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.18.0

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()

OperationBatcher

This class can simplify batch operations, for example, publishing multiple messages simultaneous. The implementation allows you to enqueue an item to run the operation on, and the operation will be scheduled in batches:

from amgi_common import OperationBatcher


async def batch_operation(items):
    # Do a batch operation to get the batch result
    return [item for item in batch_result]


operation_batcher = OperationBatcher(batch_operation)

# You can enqueue an item, it will be processed in the background automatically
result = await operation_batcher.enqueue({"an": "item"})

The batch operation should return an iterable of results, or exceptions. This allows for per item errors. The batch sizes can also be limited, which is usefully when APIs have a hard limit.

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.18.0.tar.gz (3.4 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.18.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: amgi_common-0.18.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"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}

File hashes

Hashes for amgi_common-0.18.0.tar.gz
Algorithm Hash digest
SHA256 47361374b178e07bb54bbd6239af7096bd45828e8c5cbeb105fdb0386bbcec11
MD5 b9cfa6e1e9af1d559b1c42f2b05af6ad
BLAKE2b-256 08f982249656f3f5bdb90c685c18dd154675305b26a28563b3d2e8a3ecd98339

See more details on using hashes here.

File details

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

File metadata

  • Download URL: amgi_common-0.18.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"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}

File hashes

Hashes for amgi_common-0.18.0-py3-none-any.whl
Algorithm Hash digest
SHA256 902c6355cdbf287ab8a95302eaf19cfd83d70cee9482bfd2644e84939a024d24
MD5 dda485022bcf41031b48ce17cb14ff69
BLAKE2b-256 f1f436f921d9d3ba9c9e6cc5d51f9f251bd19c2b3e27605a3a2c7f554819bdf1

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