Events
SQLAlchemy includes an event API which publishes a wide variety of hooks intothe internals of both SQLAlchemy Core and ORM.
Event Registration
Subscribing to an event occurs through a single API point, the listen()
function,or alternatively the listens_for()
decorator. These functions accept atarget, a string identifier which identifies the event to be intercepted, anda user-defined listening function. Additional positional and keyword arguments to thesetwo functions may be supported byspecific types of events, which may specify alternate interfaces for the given event function, or provideinstructions regarding secondary event targets based on the given target.
The name of an event and the argument signature of a corresponding listener function is derived froma class bound specification method, which exists bound to a marker class that’s described in the documentation.For example, the documentation for PoolEvents.connect()
indicates that the event name is "connect"
and that a user-defined listener function should receive two positional arguments:
- from sqlalchemy.event import listen
- from sqlalchemy.pool import Pool
- def my_on_connect(dbapi_con, connection_record):
- print("New DBAPI connection:", dbapi_con)
- listen(Pool, 'connect', my_on_connect)
To listen with the listens_for()
decorator looks like:
- from sqlalchemy.event import listens_for
- from sqlalchemy.pool import Pool
- @listens_for(Pool, "connect")
- def my_on_connect(dbapi_con, connection_record):
- print("New DBAPI connection:", dbapi_con)
Named Argument Styles
There are some varieties of argument styles which can be accepted by listenerfunctions. Taking the example of PoolEvents.connect()
, this functionis documented as receiving dbapi_connection
and connection_record
arguments.We can opt to receive these arguments by name, by establishing a listener functionthat accepts **keyword
arguments, by passing named=True
to eitherlisten()
or listens_for()
:
- from sqlalchemy.event import listens_for
- from sqlalchemy.pool import Pool
- @listens_for(Pool, "connect", named=True)
- def my_on_connect(**kw):
- print("New DBAPI connection:", kw['dbapi_connection'])
When using named argument passing, the names listed in the function argumentspecification will be used as keys in the dictionary.
Named style passes all arguments by name regardless of the functionsignature, so specific arguments may be listed as well, in any order,as long as the names match up:
- from sqlalchemy.event import listens_for
- from sqlalchemy.pool import Pool
- @listens_for(Pool, "connect", named=True)
- def my_on_connect(dbapi_connection, **kw):
- print("New DBAPI connection:", dbapi_connection)
- print("Connection record:", kw['connection_record'])
Above, the presence of **kw
tells listens_for()
thatarguments should be passed to the function by name, rather than positionally.
New in version 0.9.0: Added optional named
argument dispatch toevent calling.
Targets
The listen()
function is very flexible regarding targets. Itgenerally accepts classes, instances of those classes, and relatedclasses or objects from which the appropriate target can be derived.For example, the above mentioned "connect"
event acceptsEngine
classes and objects as well as Pool
classesand objects:
- from sqlalchemy.event import listen
- from sqlalchemy.pool import Pool, QueuePool
- from sqlalchemy import create_engine
- from sqlalchemy.engine import Engine
- import psycopg2
- def connect():
- return psycopg2.connect(username='ed', host='127.0.0.1', dbname='test')
- my_pool = QueuePool(connect)
- my_engine = create_engine('postgresql://ed@localhost/test')
- # associate listener with all instances of Pool
- listen(Pool, 'connect', my_on_connect)
- # associate listener with all instances of Pool
- # via the Engine class
- listen(Engine, 'connect', my_on_connect)
- # associate listener with my_pool
- listen(my_pool, 'connect', my_on_connect)
- # associate listener with my_engine.pool
- listen(my_engine, 'connect', my_on_connect)
Modifiers
Some listeners allow modifiers to be passed to listen()
. Thesemodifiers sometimes provide alternate calling signatures forlisteners. Such as with ORM events, some event listeners can have areturn value which modifies the subsequent handling. By default, nolistener ever requires a return value, but by passing retval=True
this value can be supported:
- def validate_phone(target, value, oldvalue, initiator):
- """Strip non-numeric characters from a phone number"""
- return re.sub(r'\D', '', value)
- # setup listener on UserContact.phone attribute, instructing
- # it to use the return value
- listen(UserContact.phone, 'set', validate_phone, retval=True)
Event Reference
Both SQLAlchemy Core and SQLAlchemy ORM feature a wide variety of event hooks:
Core Events - these are described inCore Events and include event hooks specific toconnection pool lifecycle, SQL statement execution,transaction lifecycle, and schema creation and teardown.
ORM Events - these are described inORM Events, and include event hooks specific toclass and attribute instrumentation, object initializationhooks, attribute on-change hooks, session state, flush, andcommit hooks, mapper initialization, object/result population,and per-instance persistence hooks.
API Reference
sqlalchemy.event.
listen
(target, identifier, fn, *args, **kw)- Register a listener function for the given target.
The listen()
function is part of the primary interface for theSQLAlchemy event system, documented at Events.
e.g.:
- from sqlalchemy import event
- from sqlalchemy.schema import UniqueConstraint
- def unique_constraint_name(const, table):
- const.name = "uq_%s_%s" % (
- table.name,
- list(const.columns)[0].name
- )
- event.listen(
- UniqueConstraint,
- "after_parent_attach",
- unique_constraint_name)
A given function can also be invoked for only the first invocationof the event using the once
argument:
- def on_config():
- do_config()
- event.listen(Mapper, "before_configure", on_config, once=True)
New in version 0.9.4: Added once=True
to event.listen()
and event.listens_for()
.
Warning
The once
argument does not imply automatic de-registrationof the listener function after it has been invoked a first time; alistener entry will remain associated with the target object.Associating an arbitrarily high number of listeners without explictitlyremoving them will cause memory to grow unbounded even if once=True
is specified.
Note
The listen()
function cannot be called at the same timethat the target event is being run. This has implicationsfor thread safety, and also means an event cannot be addedfrom inside the listener function for itself. The list ofevents to be run are present inside of a mutable collectionthat can’t be changed during iteration.
Event registration and removal is not intended to be a “highvelocity” operation; it is a configurational operation. Forsystems that need to quickly associate and deassociate withevents at high scale, use a mutable structure that is handledfrom inside of a single listener.
Changed in version 1.0.0: - a collections.deque()
object is nowused as the container for the list of events, which explicitlydisallows collection mutation while the collection is beingiterated.
See also
sqlalchemy.event.
listensfor
(_target, identifier, *args, **kw)- Decorate a function as a listener for the given target + identifier.
The listens_for()
decorator is part of the primary interface for theSQLAlchemy event system, documented at Events.
e.g.:
- from sqlalchemy import event
- from sqlalchemy.schema import UniqueConstraint
- @event.listens_for(UniqueConstraint, "after_parent_attach")
- def unique_constraint_name(const, table):
- const.name = "uq_%s_%s" % (
- table.name,
- list(const.columns)[0].name
- )
A given function can also be invoked for only the first invocationof the event using the once
argument:
- @event.listens_for(Mapper, "before_configure", once=True)def on_config(): do_config()
New in version 0.9.4: Added once=True
to event.listen()
and event.listens_for()
.
Warning
The once
argument does not imply automatic de-registrationof the listener function after it has been invoked a first time; alistener entry will remain associated with the target object.Associating an arbitrarily high number of listeners without explictitlyremoving them will cause memory to grow unbounded even if once=True
is specified.
See also
listen()
- general description of event listening
The arguments here should match exactly those which were sent tolisten()
; all the event registration which proceeded as a resultof this call will be reverted by calling remove()
with the samearguments.
e.g.:
- # if a function was registered like this...
- @event.listens_for(SomeMappedClass, "before_insert", propagate=True)
- def my_listener_function(*arg):
- pass
- # ... it's removed like this
- event.remove(SomeMappedClass, "before_insert", my_listener_function)
Above, the listener function associated with SomeMappedClass
was alsopropagated to subclasses of SomeMappedClass
; the remove()
function will revert all of these operations.
New in version 0.9.0.
Note
The remove()
function cannot be called at the same timethat the target event is being run. This has implicationsfor thread safety, and also means an event cannot be removedfrom inside the listener function for itself. The list ofevents to be run are present inside of a mutable collectionthat can’t be changed during iteration.
Event registration and removal is not intended to be a “highvelocity” operation; it is a configurational operation. Forsystems that need to quickly associate and deassociate withevents at high scale, use a mutable structure that is handledfrom inside of a single listener.
Changed in version 1.0.0: - a collections.deque()
object is nowused as the container for the list of events, which explicitlydisallows collection mutation while the collection is beingiterated.
See also
sqlalchemy.event.
contains
(target, identifier, fn)- Return True if the given target/ident/fn is set up to listen.
New in version 0.9.0.