celery的task返回response对象报错

发布于 2022-09-04 03:36:59 字数 2001 浏览 15 评论 0

celery配置为:

app.config.update(
    CELERY_BROKER_URL='amqp://guest:guest@localhost:5672//',
    CELERY_RESULT_BACKEND='redis://localhost:6379/0'
)
celery = make_celery(app)

celery.conf.update(
    CELERY_TASK_SERIALIZER='pickle',
    CELERY_RESULT_SERIALIZER = 'pickle',
    RYD_POOL = 'gevent')

有些奇怪是因为,celery和flask结合在一起,代码和问题无关就不贴了。
用celery做一个爬取的任务。代码如下

@celery.task()
def fetcher(url):
    return requests.get('http://'+url)
    

请求报错:

[2016-11-14 14:58:23,075: ERROR/MainProcess] 
Task celerytask.fetcher[dfc93f01-cdeb-40fb-a42a-cbf9c9cbf6e1] 
raised unexpected: TypeError("object of type 'Response' has no len()",)Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 425, in trace_taskparent_id=uuid, root_id=root_id,File "/usr/local/lib/python2.7/dist-packages/celery/canvas.py", line 221, in apply_asyncreturn _apply(args, kwargs, **options)
File "/usr/local/lib/python2.7/dist-packages/celery/app/task.py", line 536, in apply_async**optionsFile "/usr/local/lib/python2.7/dist-packages/celery/app/base.py", line 709, in send_taskroot_id, parent_id, shadow, chain,
File "/usr/local/lib/python2.7/dist-packages/celery/app/amqp.py", line 333, in as_task_v2argsrepr = saferepr(args, self.argsrepr_maxsize)
File "/usr/local/lib/python2.7/dist-packages/celery/utils/saferepr.py", line 74, in saferepro, maxlen=maxlen, maxlevels=maxlevels, seen=seenFile "/usr/local/lib/python2.7/dist-packages/celery/utils/saferepr.py", line 104, in _safereprfor token, it in reprstream(stack, seen=seen, maxlevels=maxlevels):
File "/usr/local/lib/python2.7/dist-packages/celery/utils/saferepr.py", line 155, in reprstreamfor val in it:
File "/usr/local/lib/python2.7/dist-packages/celery/utils/saferepr.py", line 91, in _chainlistsize = len(it)
TypeError: object of type 'Response' has no len()

该怎么解决呢?
调用为:

 fetcher.delay(url)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文