mlinsenbard
mlinsenbard

Reputation: 49

Celery task keeps thinking Django model instance is an integer object

I am completely baffled by this, but maybe another pair of eyes can help.

I'm passing a Model's primary_key into a Celery task that is launched via my_task.delay(args). Here's the code inside the task:

from my_project.my_app.models import *

@task()
def mytask(arg1, arg2, primary_key):
    m = Machine.objects.get(pk=primary_key)

    if m.last_os:
        last_ver = m.last_os.split('_')[1]
    else:
        last_ver = 'None'

And Celery is furious that I would even try to do this and spits out this error:

Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/celery/app/trace.py", line 238, in trace_task
R = retval = fun(*args, **kwargs)
File "/Library/Python/2.7/site-packages/celery/app/trace.py", line 416, in  __protected_call__
return self.run(*args, **kwargs)
File "/Users/me/Documents/python/my_project/my_project/my_app/tasks.py", line 33, in my_task
@task()
AttributeError: 'int' object has no attribute 'last_os'

I verify the pk that's passed to the function is indeed an int (its 1 and I'm using django's default pk assignments).

Another thing to note is that if I pass the Model instance in itself, everything works peachy keen - I just would rather not have a possibly outdated model object flying around my Celery tasks.

Any help is appreciated.

Edit: I've also tried hardcoding a pk into the Machine.objects.get(pk=1) call and it STILL thinks it's an integer, while I get a perfectly good Machine instance in the shell.

Upvotes: 0

Views: 566

Answers (1)

mlinsenbard
mlinsenbard

Reputation: 49

So it turns out that when you edit a task while the Celery service is running, those changes don't reflect on the running Celery service. So whenever you change a celery task's code, you need to restart the Celery service.

A shame the starting guides never specify this.

Upvotes: 1

Related Questions