On Wednesday, April 11, 2012 12:19:14 PM UTC-4, Michael Bayer wrote:
>
>
> On Apr 11, 2012, at 12:12 PM, Paddy Mullen wrote:
>
> > I could have sworn I saw conflicting behaviour.
> > 
> >  My after_insert would run, and the instance would have an id, it would 
> then enqueue a task in celery.  On the other side (in another process), 
> celery would run that task, make a query against that database for that ID, 
> and the ID wouldn't be in the database.
>
> oh, that's completely different.   Your flush is inside of a transaction. 
>   Your newly generated id *will not* be exposed to the outside world until 
> the transaction commits.    If you're spawning off celery tasks that are 
> dependent on the newly flushed data, you *must* use after_commit() for 
> that.  Not just for isolation purposes, but what if your transaction failed 
> and was rolled back - then celery would be failing there as well.
>
 

Thanks, That was the problem.  I think I have it working now.  I will 
update my repo for you to take a look at later.  
Thanks, that was the problem.  I think I have it working now.  I will 
update my repo for future examples later today.

To make it work, I listen to the after_insert event and append callback 
functions onto the "do_its" property of  the model instance.
Then I listen to "after_flush" on the session object, here I iterate 
through dirty and new objects and grab their "do_its" appending them onto 
the session.
I also listen to "after_commit" on the session object.  Here I iterate 
through "do_its" and call them, then I clear the "do_its" list on the 
session object.
I should also clear the "do_its" on the session object on the 
"after_soft_rollback" event.

It seems like I might have a common usecase here.  What would you think of 
adding  "after_insert_committed", "after_update_committed", and after 
"delete_committed" events to the Table object.  I would be interested in 
taking a crack at it.

Thanks,
Paddy

 
 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/Y3kT4VDNgSwJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to