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.
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.
精彩评论