Celery 错误:“str 对象不可调用”,getstate() 函数是一个字符串

发布于 2024-11-18 02:24:43 字数 2200 浏览 4 评论 0原文

当我尝试将任务传递给 Celery 工作人员时,我收到此错误。这是回溯

Traceback:
File "/home/vivek/xpython/lib/python2.6/site-packages/Django-1.3-py2.6.egg/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)

File "/home/vivek/projects/engine/engine/web/models.py" in fb_sync
53.         process_metadata.delay(self, wrapper)

File "/home/vivek/xpython/lib/python2.6/site-packages/celery-2.2.7-py2.6.egg/celery/task/base.py" in delay
338.         return self.apply_async(args, kwargs)

File "/home/vivek/xpython/lib/python2.6/site-packages/celery-2.2.7-py2.6.egg/celery/task/base.py" in apply_async
460.                                          **options)

File "/home/vivek/xpython/lib/python2.6/site-packages/celery-2.2.7-py2.6.egg/celery/app/amqp.py" in delay_task
230.         send(body, exchange=exchange, **extract_msg_options(kwargs))

File "/home/vivek/xpython/lib/python2.6/site-packages/kombu-1.1.6-py2.6.egg/kombu/compat.py" in send
101.         return self.publish(*args, **kwargs)

File "/home/vivek/xpython/lib/python2.6/site-packages/kombu-1.1.6-py2.6.egg/kombu/messaging.py" in publish
124.                 compression, headers)

File "/home/vivek/xpython/lib/python2.6/site-packages/kombu-1.1.6-py2.6.egg/kombu/messaging.py" in _prepare
147.              body) = encode(body, serializer=serializer)

File "/home/vivek/xpython/lib/python2.6/site-packages/kombu-1.1.6-py2.6.egg/kombu/serialization.py" in encode
119.         payload = encoder(data)

File "/home/vivek/xpython/lib/python2.6/copy_reg.py" in _reduce_ex
84.         dict = getstate()



 Exception Type: TypeError at /login/
  Exception Value: 'str' object is not callable

    /home/vivek/xpython/lib/python2.6/copy_reg.py in _reduce_ex
        dict = getstate() ...

local variables
 {'args': (<web.models.User object at 0x8b47b4c>,<web.fb.Facebook object at 0x8b4fbac>),
 'eta': None,
 'expires': None,
 'id': '8d8e6c0b-a269-4780-9c48-77e689037322',
 'kwargs': {},
 'retries': 0,
 'task': 'web.models.process_likes'}


What is the solution to this problem? When I remove the task decorator, it works fine.

I'm getting this error when I try to pass a task to a Celery worker. Here is the traceback

Traceback:
File "/home/vivek/xpython/lib/python2.6/site-packages/Django-1.3-py2.6.egg/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)

File "/home/vivek/projects/engine/engine/web/models.py" in fb_sync
53.         process_metadata.delay(self, wrapper)

File "/home/vivek/xpython/lib/python2.6/site-packages/celery-2.2.7-py2.6.egg/celery/task/base.py" in delay
338.         return self.apply_async(args, kwargs)

File "/home/vivek/xpython/lib/python2.6/site-packages/celery-2.2.7-py2.6.egg/celery/task/base.py" in apply_async
460.                                          **options)

File "/home/vivek/xpython/lib/python2.6/site-packages/celery-2.2.7-py2.6.egg/celery/app/amqp.py" in delay_task
230.         send(body, exchange=exchange, **extract_msg_options(kwargs))

File "/home/vivek/xpython/lib/python2.6/site-packages/kombu-1.1.6-py2.6.egg/kombu/compat.py" in send
101.         return self.publish(*args, **kwargs)

File "/home/vivek/xpython/lib/python2.6/site-packages/kombu-1.1.6-py2.6.egg/kombu/messaging.py" in publish
124.                 compression, headers)

File "/home/vivek/xpython/lib/python2.6/site-packages/kombu-1.1.6-py2.6.egg/kombu/messaging.py" in _prepare
147.              body) = encode(body, serializer=serializer)

File "/home/vivek/xpython/lib/python2.6/site-packages/kombu-1.1.6-py2.6.egg/kombu/serialization.py" in encode
119.         payload = encoder(data)

File "/home/vivek/xpython/lib/python2.6/copy_reg.py" in _reduce_ex
84.         dict = getstate()



 Exception Type: TypeError at /login/
  Exception Value: 'str' object is not callable

    /home/vivek/xpython/lib/python2.6/copy_reg.py in _reduce_ex
        dict = getstate() ...

local variables
 {'args': (<web.models.User object at 0x8b47b4c>,<web.fb.Facebook object at 0x8b4fbac>),
 'eta': None,
 'expires': None,
 'id': '8d8e6c0b-a269-4780-9c48-77e689037322',
 'kwargs': {},
 'retries': 0,
 'task': 'web.models.process_likes'}


What is the solution to this problem? When I remove the task decorator, it works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何必那么矫情 2024-11-25 02:24:43

您正尝试将对象传递给无法序列化的任务。尝试传递更简单的数据(让您在任务运行时重新创建复杂对象的信息)。

不确定这是否是错误的实际原因,但无论如何您都不应该将 Django 模型传递给任务。否则你会遇到竞争条件。请参阅有关该主题的 Celery 文档

它们不应该作为参数传递给任务。当任务运行时从数据库重新获取对象几乎总是更好,因为使用旧数据可能会导致竞争条件。

将用户对象的唯一键以及识别 Facebook Like 交互所需的任何内容传递为简单的 Python 类型,并使用任务中的该信息来重新创建执行该任务所需的内容。

You are trying to pass an object to a task that cannot be serialized. Try to pass simpler data (information that let's you recreate the complex objects when the task is run).

Not sure if that's the actual cause of your error, but you should not pass Django models to tasks anyway. You will run into race conditions otherwise. See the Celery documentation on the subject:

They shouldn’t be passed on as arguments to tasks. It’s almost always better to re-fetch the object from the database when the task is running instead, as using old data may lead to race conditions.

Pass the unique key for the user object and whatever you need to identify the Facebook Like interaction as simple python types instead, and use that information from within the task to recreate what you need to do the task instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文