Skip to main content

Topical

A simple, lightweight event pipeline in Python.

Installation

pip install ap-topical

Usage

import logging, asyncio
import ap.topical
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

@ap.topical.event('start')
async def start_handler(payload):
    """Do stuff here"""

    await ap.topical.publish('end', payload)


@ap.topical.event('start')
async def start_auditor(payload):
    logger.info('auditing start event')

@ap.topical.event('end')
async def end_handler(payload):
    """Do stuff here"""


async main():
    await ap.topical.publish('start', {})

if __name__ == '__main__':
    asyncio.run(main())

Events

Events are strings (or other hashable objects) that are published via ap.topical.publish(). These are used as both a topic and a specific event.

Subscribing Callbacks to Events

You can subscribe a callback to a given event using ap.topical.subscribe as in the following example:

async def my_event_handler(payload):
    ...

ap.topical.subscribe('my-event', my_event_handler)

Additionally, you can use the handy @ap.topical.event() decorator to do this for you automatically:

@ap.topical.event('my-event')
async def my_event_handler(payload):
    ...

Publishing Events

Events can be published by providing an event and a payload to pass between event handlers.

await ap.topical.publish('my-event', {})

Multiple Handlers

A given event can have any number of handlers (limited by available compute resources). These will all trigger asynchronously.

@ap.topical.event('start')
async def start_handler(payload):
    ...

@ap.topical.event('start')
async def start_auditor(payload):
    ...

await ap.topical.publish('start', {})

This allows you to create robust graphs of event handlers.

Future Plans

  • add unsubscribe functionality
  • add wildcard event support
  • add default event payload support
  • add payload validation via msgspec

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

ap_topical-0.1.0-py3-none-any.whl (2.8 kB view details)

Uploaded Python 3

File details

Details for the file ap_topical-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ap_topical-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 2.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.4

File hashes

Hashes for ap_topical-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 854565217e218da897c3970e77078f21b9197c821749549dc2a0d6134fae804d
MD5 3cf045a6e530aaa035a3c952e5dac225
BLAKE2b-256 db76df9162bcb223c743283184cab6fee9b88df13dee95154bbf3543efc50208

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 Sentry Error logging StatusPage Status page