On Monday, 25 November 2013 17:27:02 UTC+2, Michael Bayer wrote: > > > On Nov 25, 2013, at 9:00 AM, George Sakkis <george...@gmail.com<javascript:>> > 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<http://www.google.com/url?q=http%3A%2F%2Fnew_user.name&sa=D&sntz=1&usg=AFQjCNHUYRuAKfHoBRBgPDAOZm4sTiKzOg>)) > > > > 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) >
Nice, this should do the trick, thanks. Any workaround for 0.7.x-0.8.x ? George -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.