简体   繁体   English

Celery任务一直认为Django模型实例是一个整数对象

[英]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) . 我正在将模型的primary_key传递到通过my_task.delay(args)启动的Celery任务中。 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). 我验证传递给该函数的pk确实是一个int(其值为1,我正在使用django的默认pk分配)。

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. 还要注意的另一件事是,如果我自己传递Model实例,那么一切都会如愿以偿-我只是不希望在Celery任务中出现过时的模型对象。

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. 编辑:我也尝试过将pk硬编码到Machine.objects.get(pk=1)调用中,它仍然认为它是整数,而我在shell中得到了一个非常好的Machine实例。

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. 事实证明,当您在Celery服务运行时编辑任务时,这些更改不会反映在正在运行的Celery服务上。 So whenever you change a celery task's code, you need to restart the Celery service. 因此,无论何时更改celery任务的代码,都需要重新启动Celery服务。

A shame the starting guides never specify this. 令人遗憾的是,入门指南从未明确指出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM