Signals provide a way for one part of your application to tell another part that something happened.

async def send_registration_email(**context):
    await send_email(context["email"], template="registration")"/register")
async def handle_registration(request):
    await do_registration(request)

Adding a signal#

The API for adding a signal is very similar to adding a route.

async def my_signal_handler():
    print("something happened")

app.add_signal(my_signal_handler, "something.happened.ohmy")

But, perhaps a slightly more convenient method is to use the built-in decorators.

async def my_signal_handler():
    print("something happened")

If the signal requires conditions, make sure to add them while adding the handler.

async def my_signal_handler1():
    print("something happened")

    conditions={"some_condition": "value"}

@app.signal("something.happened.ohmy2", conditions={"some_condition": "value"})
async def my_signal_handler2():
    print("something happened")

Signals can also be declared on blueprints

bp = Blueprint("foo")

async def my_signal_handler():
    print("something happened")

Built-in signals#

In addition to creating a new signal, there are a number of built-in signals that are dispatched from Sanic itself. These signals exist to provide developers with more opportunities to add functionality into the request and server lifecycles.

Added in v21.9

You can attach them just like any other signal to an application or blueprint instance.

async def my_signal_handler(conn_info):
    print("Connection has been closed")

These signals are the signals that are available, along with the arguments that the handlers take, and the conditions that attach (if any).

Event name Arguments Conditions
http.routing.before request
http.routing.after request, route, kwargs, handler
http.handler.before request
http.handler.after request
http.lifecycle.begin conn_info
http.lifecycle.read_head head
http.lifecycle.request request
http.lifecycle.handle request
http.lifecycle.read_body body
http.lifecycle.exception request, exception
http.lifecycle.response request, response
http.lifecycle.send data
http.lifecycle.complete conn_info
http.middleware.before request, response {"attach_to": "request"} or {"attach_to": "response"}
http.middleware.after request, response {"attach_to": "request"} or {"attach_to": "response"} app, exception
server.init.before app, loop
server.init.after app, loop
server.shutdown.before app, loop
server.shutdown.after app, loop

Version 22.9 added http.handler.before and http.handler.after.

Version 23.6 added

To make using the built-in signals easier, there is an Enum object that contains all of the allowed built-ins. With a modern IDE this will help so that you do not need to remember the full list of event names as strings.

Added in v21.12

from sanic.signals import Event

async def my_signal_handler(conn_info):
    print("Connection has been closed")


Signals are based off of an event. An event, is simply a string in the following pattern:


Events must have three parts. If you do not know what to use, try these patterns:

  • my_app.something.happened
  • sanic.notice.hello

Event parameters#

An event can be "dynamic" and declared using the same syntax as path parameters. This allows matching based upon arbitrary values.

async def signal_handler(thing):
    print(f"[signal_handler] {thing=}")

async def trigger(request):
    await app.dispatch("")
    return response.text("Done.")

Checkout path parameters for more information on allowed type definitions.

Only the third part of an event (the action) may be dynamic:

  •<thing> πŸ†—
  • foo.<bar>.baz ❌


In addition to executing a signal handler, your application can wait for an event to be triggered.

await app.event("")

IMPORTANT: waiting is a blocking function. Therefore, you likely will want this to run in a background task.

async def wait_for_event(app):
    while True:
        print("> waiting")
        await app.event("")
        print("> event found\n")

async def after_server_start(app, loop):

If your event was defined with a dynamic path, you can use * to catch any action.



await app.event("*")


In the future, Sanic will dispatch some events automatically to assist developers to hook into life cycle events.

Dispatching an event will do two things:

  1. execute any signal handlers defined on the event, and
  2. resolve anything that is "waiting" for the event to complete.
async def foo_bar(thing):

await app.dispatch("")


Sometimes you may find the need to pass extra information into the signal handler. In our first example above, we wanted our email registration process to have the email address for the user.

async def send_registration_email(**context):

await app.dispatch(
    context={"hello": "world"}
{'hello': 'world'}


Signals are dispatched in a background task.


Dispatching blueprint signals works similar in concept to middleware. Anything that is done from the app level, will trickle down to the blueprints. However, dispatching on a blueprint, will only execute the signals that are defined on that blueprint.

Perhaps an example is easier to explain:

bp = Blueprint("bp")

app_counter = 0
bp_counter = 0

def app_signal():
    nonlocal app_counter
    app_counter += 1

def bp_signal():
    nonlocal bp_counter
    bp_counter += 1

Running app.dispatch("") will execute both signals.

await app.dispatch("")
assert app_counter == 1
assert bp_counter == 1

Running bp.dispatch("") will execute only the blueprint signal.

await bp.dispatch("")
assert app_counter == 1
assert bp_counter == 2