kombu.exceptions.EncodeError: Object of type FileStorage is not JSON serializable
在项目的setting中增加这样的配置,才可以
CELERY_TASK_SERIALIZER = 'pickle'
CELERY_RESULT_SERIALIZER = 'pickle'
CELERY_ACCEPT_CONTENT = ['pickle', 'json']
报错原因:
celery4版本的 默认使用 JSON 作为 serializer ,而 celery3版本的默认使用 pickle。
所以为了让报错消除,需要添加以上设置。
最好问题得到成功解决:运行如下结果
kombu.exceptions.EncodeError: cannot serialize ‘_io.BufferedReader’ object 的分析解决
#python代码如下
from celery import Celery
import subprocess
app = Celery('tasks',
broker='redis://localhost',
backend='redis://localhost')
@app.task
def add(x,y):
print("running...",x,y)
return x+y
@app.task
def run_cmd(cmd):
cmd_obj=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
return cmd_obj.stdout.read()
[python@qinhan file]$ python
Python 3.6.2 (default, Jan 10 2018, 16:15:47)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import task
>>> task.run_cmd('df') #此条命令不会发送到celery后台
b'Filesystem 1K-blocks Used Available Use% Mounted on\n/dev/mapper/centos-root 5232640 2481244 2751396 48% /\ndevtmpfs 490308 0 490308 0% /dev\ntmpfs 500680 0 500680 0% /dev/shm\ntmpfs 500680 6968 493712 2% /run\ntmpfs 500680 0 500680 0% /sys/fs/cgroup\n/dev/mapper/centos-home 2086912 33076 2053836 2% /home\n/dev/mapper/centos-var 975532 633120 342412 65% /var\n/dev/sda1 508588 126972 381616 25% /boot\ntmpfs 100136 0 100136 0% /run/user/0\n'
>>> task.run_cmd.delay('df') #此条命令会发送到celery后台,celery后台报错not JSON serializable
<AsyncResult: 2c671a6b-be73-4f46-8b55-c58f0a05ed2f>
>>>
情况说明:celery 默认解析为 pickle
class xxxxx():
def __init__(self,request):
self.request = request (这个是 django 请求的 requst <WSGIRequest: POST '/server/xxxxx/'>,最后把这个去掉就好了,)
def funcA(self,x,y):
return x,y
def funcB(self):
# website_request 这是我 app 下的 tasks.py 里的一个函数 ,然后把它放入后台,根据返回结果去调用 self.funcA
website_request.apply_async(kwargs={'fun':self.funcA,'x':1,'y':1})
把函数传入后台时,不要在初始化的时候,传入一些 pickle 不能解析的函数,列如 接受 django 请求的 request,其他的目前未发现
flask redis Authentication required.
宝塔看看是否加密了,或者看看自己设置的链接是否正确
lask redis consumer: Cannot connect to amqp://guest:**@127.0.0.1:5