Flask and Signals


I’ve been using signals heavily within Sentry and really like the pattern. Making note of how to do this in Flask and SQLAlchemy for my own projects.

from sqlalchemy import event

@event.listens_for(User, 'after_insert')
def after_insert_listener(mapper, connection, target):
    assert type(target) == User

SQLAlchemy ORM events

from blinker import signal

test_signal = signal('test-signal')

@test_signal.connect
def test_function(sender, key, *args, **kwargs):
    print(sender)
    print(key)
    print(args)
    print(kwargs)

Blinker events