On Nov 25, 2013, at 9:00 AM, George Sakkis <george.sak...@gmail.com> wrote:

> Hi all,
> 
> is there a feature or pattern for adding a listener that is to be executed 
> (at most) once? For example, say you want to send an email when user is 
> created and the session is committed. If event.listen() supported a "once" 
> boolean parameter, this could be expressed as:
> 
> def email_user(email_address, message):
>    ...
> ...
> if  valid_data:
>     new_user = User(**valid_data)
>     db.session.add(new_user)
>     # send an email to new_user after committing
>     event.listen(db.session, "after_commit", 
>                        once=True, # hypothetical parameter
>                        lambda session: email_user(new_user.email, "welcome 
> {}!".format(new_user.name))

there are certain events such as pool.first_connect that use a “once” type of 
system, but that is linked to the type of event.

I haven’t tried this but you should be able to call event.remove() (sqlalchemy 
0.9) within the handler:

def go(session):
    email_user(…)
    event.remove(session, “after_commit”, go)
event.listen(session, “after_commit”, go)


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to