for loop + transaction.on_commit + celery = Unexpected Behaviour

315 views
Skip to first unread message

Emilio Jimenez Canalejo

unread,
Apr 24, 2017, 9:17:48 AM4/24/17
to Django users
Hi, I've been looking for some time around the internet a problem I have, maybe I'm just dull from all the searching and the answer is pretty obvious, but I can't find it.

For example, I have code like this:
@app.task
def foo(idx):
   
print("This is iteration {}".format(idx))

def save_model(self, request, obj, form, change):
    data = request.POST
    result = super(Model, self).save_model(request, obj, form, change)

   
for pk in data['_models_changed']:
        transaction
.on_commit(lambda: foo(pk))

To my astonishment, I thought I would get:
"This is iteration 1"
"This is iteration 2"
"This is iteration 3"
"This is iteration 4"

But I got:
"This is iteration 4"
"This is iteration 4"
"This is iteration 4"
"This is iteration 4"

I'm using python 2.7, Django 1.10.5 and Celery 4.0.2.

'models_changed' is a field in the post added by a custom clean method.

Anyone knows a way to do this?

Thanks for all!

m712 - Developer

unread,
Apr 24, 2017, 9:50:43 AM4/24/17
to Emilio Jimenez Canalejo, Django users

I'm not too familiar with Celery, but I think that for loop continuously keeps changing transaction's commit event handler. Maybe you are looking for something like transaction.commit instead of on_commit?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/91e87fb4-69e2-4b4e-bd6b-704eb7567026%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Emilio Jimenez Canalejo

unread,
Apr 24, 2017, 10:35:43 AM4/24/17
to Django users, emj...@gmail.com, com...@getbackinthe.kitchen
The commit event handler is to commit a transaction, the on_commit event is for running a function when the commit is done. That is my intention, but maybe there is something I don't see.

I want to execute a celery task, foo.delay(...), when the save of Model is finished. That way if I modify Model inside the task, it will have the changes applied. If that is not the case, there are sometimes that I could get the Model instance before the save finished, and it would be the old instance.

Jani Tiainen

unread,
Apr 27, 2017, 2:27:11 AM4/27/17
to django...@googlegroups.com

Hi,

I think your problem is to use POST['_models_changed']. Django uses specialized version of dictionary there which return _last_ value from list. You need to use special "get_list()" method to get whole list of objects.

Your code should start working if you do data['_models_changed'].get_list()  in a for-loop.

Also, why you modify request.POST instead of adding class attribute?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/91e87fb4-69e2-4b4e-bd6b-704eb7567026%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Jani Tiainen

Martin Pätzold

unread,
Dec 3, 2021, 12:53:09 PM12/3/21
to Django users
This is an older topic, but for everyone who found this discusion and had the same issue: The problem is most likely using "lambda" inside a for-loop. If you switch out "lambda" for "functools.partial" it should work as expected.
Reply all
Reply to author
Forward
0 new messages