Skip to main content

Cortex Axon SDK for Python

This is the official Cortex Axon SDK for Python. It provides a simple way to interact with and extend your Cortex instance.

Getting started

To run the Cortex SDK you need to get the Axon Agent via Docker:

docker pull ghcr.io/cortexapps/cortex-axon-agent:latest

Then to scaffold a Python project:

docker run -it --rm -v "$(pwd):/src" ghcr.io/cortexapps/cortex-axon-agent:latest init --language python my-python-axon project

This will create a new directory my-python-axon with a Go project scaffolded in the current directory.

Running locally

To run your project, first start the agent Docker container like:

docker run -it --rm -p "50051:50051" -p "80:80" -e "DRYRUN=true" ghcr.io/cortexapps/cortex-axon-agent:latest serve

This is DRYRUN mode that prints what it would have called, to run against the Cortex API remove the DRYRUN environment variable and add -e "CORTEX_API_TOKEN=$CORTEX_API_TOKEN. Be sure to export your token first, e.g. export CORTEX_API_TOKEN=your-token.

Adding handlers

To add a handler, open main.py and create a function:

@cortex_scheduled(interval="5s")
def my_handler(ctx: HandlerContext):

    payload = {
        "values": {
            "my-service": [
                {
                    "key": "exampleKey1",
                    "value": "exampleValue1",
                },
                {
                    "key": "exampleKey2",
                    "value": "exampleValue2",
                },
            ]
        }
    }

    json_payload = json.dumps(payload)

    response = ctx.cortex_api_call(
        method="PUT",
        path="/api/v1/catalog/custom-data",
        body=json_payload,
    )

    if response.status_code >= 400:
        ctx.log(f"SetCustomTags error: {response.body}", level="ERROR")
        exit(1)

    ctx.log("CortexApi PUT custom-data called successfully!")

@cortex_webhook(id="my-webhook-1")
def my_webhook_handler(context: HandlerContext):
    context.log("Success! Webhook handler called!")
    body = context.args["body"]
    context.log(f"Webhook body: {body}")

Now start the agent in a separate terminal:

make run-agent

And run your project:

python main.py

This will begin executing your handler every 5 seconds. To invoke the webhook handler, you can just send a POST request to http://localhost:80/webhook/my-webhook-1 with a body.

Handler concurrency

Handlers run on a thread pool, so a slow handler does not delay the ones behind it. Eight run at once by default. Once every worker is busy, the SDK stops reading new invocations until one finishes.

Write handlers to be thread safe, because two invocations of the same handler can run at the same time. To change the limit:

client = AxonClient(scope=globals(), max_concurrent_handlers=16)

Set it to 1 for the old behaviour, where handlers run one at a time.

Metadata

Release files for cortex-axon-sdk 0.1.0

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

Source distribution (sdist)

Source distribution for cortex-axon-sdk 0.1.0
File Size Uploaded
cortex_axon_sdk-0.1.0.tar.gz 13.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cortex-axon-sdk 0.1.0
File Interpreter ABI Platform
cortex_axon_sdk-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 32.9 kB

Release files / cortex_axon_sdk-0.1.0.tar.gz

Download URL cortex_axon_sdk-0.1.0.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a501fd1e1b0568724400f72803ffbe78bcd1b6dd72dadc9538de467f2b18aeac
BLAKE2b-256 checksum
How to use checksums
c93d99ff891aed46ae6e8ae4bc11171382ee6464572fc1ed195106c5586ea603
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.0 CPython/3.13.15 Linux/6.17.0-1022-azure

Release files / cortex_axon_sdk-0.1.0-py3-none-any.whl

Download URL cortex_axon_sdk-0.1.0-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5193a011b3a37847d2b37ecc9a6bc92967aeaec46f6038ef3a956d4f7494ffdd
BLAKE2b-256 checksum
How to use checksums
b8586f463ecae674be0c7e535afccb32584e96a1e71dce2583c39731f41bc6fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.0.0 CPython/3.13.15 Linux/6.17.0-1022-azure

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

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