我将celery 4.2.0与Django一起使用,以在rabbitmq中计划新任务,并且在解析参数时随机失败。 我的代码如下:
for param in serializer.validated_data['parameters']:
run_task.delay(param_name=param)
其中参数是int的列表,并通过序列化程序验证来确保。
@shared_task
def run_task(param_name: int) -> None:
obj= Obj.objects.filter(param=param_name)
...
在UT下,当我用run_task
调用此CELERY_TASK_ALWAYS_EAGER=True
时,一切正常。在实际环境中,此任务有时可以正常运行,有时会因服务器错误而失败:
File "/usr/lib/python3.6/site-packages/rest_framework/decorators.py", line 53, in handler
return func(*args, **kwargs)
File "/opt/app/compliance/jobs/views.py", line 30, in create_task
run_task.delay(param_name=param)
File "/usr/lib/python3.6/site-packages/celery/app/task.py", line 408, in delay
return self.apply_async(args, kwargs)
File "/usr/lib/python3.6/site-packages/celery/app/task.py", line 535, in apply_async
**options
File "/usr/lib/python3.6/site-packages/celery/app/base.py", line 741, in send_task
with self.producer_or_acquire(producer) as P:
File "/usr/lib/python3.6/site-packages/celery/app/base.py", line 876, in producer_or_acquire
producer, self.producer_pool.acquire, block=True,
File "/usr/lib/python3.6/site-packages/celery/app/base.py", line 1246, in producer_pool
return self.amqp.producer_pool
File "/usr/lib/python3.6/site-packages/celery/app/amqp.py", line 612, in producer_pool
self.app.connection_for_write()]
File "/usr/lib/python3.6/site-packages/kombu/utils/collections.py", line 34, in __getitem__
h = eqhash(key)
File "/usr/lib/python3.6/site-packages/kombu/utils/collections.py", line 25, in eqhash
return o.__eqhash__()
File "/usr/lib/python3.6/site-packages/kombu/connection.py", line 629, in __eqhash__
repr(self.transport_options))
File "/usr/lib/python3.6/site-packages/kombu/utils/collections.py", line 16, in __init__
self.hashvalue = hash(seq)
TypeError: unhashable type: 'set'
我理解错误本身,但是当我仅传递一个简单且可分析的参数(未设置)时,错误会在这里出现。有谁知道如何使其持久地工作?
谢谢!