python 如何配置“celepie”以使用串行器“pickle”?

vh0rcniy  于 2022-12-17  发布在  Python
关注(0)|答案(1)|浏览(130)

在tasks.py端,我执行以下操作:

app = Celery(
        main='tasks',
        backend='rpc://',
        broker='pyamqp://USERNAME:PASSWORD@localhost',
        )
app.conf.task_serializer = 'pickle'
app.conf.result_serializer = 'pickle'
app.conf.event_serializer = 'pickle'
app.conf.accept_content = ['pickle']
app.conf.task_accept_content = ['pickle']
app.conf.result_accept_content = ['pickle']
app.conf.event_accept_content = ['pickle']

当我通过以下方式手动启动两个worker时:

celery --app tasks worker --concurrency=1 -n by_hand_1 &
sleep 5
celery --app tasks worker --concurrency=1 -n by_hand_2

我得到错误:

[ERROR/MainProcess] Refusing to deserialize disabled content of type pickle (application/x-python-serialize

如何配置celery以使用序列化程序“pickle”?

关于“重复”,celery框架已经发生了很大的变化,从4.0版本开始,默认的串行化器从pickle更改为默认的json ......所以这就是为什么我问了一个新问题。

flmtquvp

flmtquvp1#

使用MIME类型配置应用程序似乎可以解决此问题:

app.conf.event_serializer = 'pickle' # this event_serializer is optional. somehow i missed this when writing this solution and it still worked without.
app.conf.task_serializer = 'pickle'
app.conf.result_serializer = 'pickle'
app.conf.accept_content = ['application/json', 'application/x-python-serialize']

以前,我试图设置接受conect字段只有“pickle”,这没有帮助。
另外,这里还有一些关于是否需要pickle序列化器的注意事项:

  • 如果您需要发送json-non-serializable对象,则还需要使用“pickle”配置任务序列化程序
  • 如果结果具有json不可序列化对象,则需要将结果序列化程序配置为“pickle”
  • accept_content设置为上述值意味着您可以发送jsonpicklecelery将在消息报头中看到类型并执行正确的操作

相关问题