我可以在celery上使用Python请求吗? [英] Can I use Python requests with celery?

查看:57
本文介绍了我可以在celery上使用Python请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为task.py的celery模块中定义了以下内容,并导入了请求库:

I have the following defined in a celery module named tasks.py with the requests library imported:

@celery.task
def geturl(url):
    res = requests.get(url)
    return res.content

每当我通过以下方式调用任务(通过task.py或REPL)时:

Whenever I call the task (either from tasks.py or REPL) with:

res = geturl.delay('http://www.google.com')
print res.get()

以下是celery服务器上的日志条目:

Here are the log entries on the celery server:

[2012-12-19 18:49:58,400:INFO/MainProcess]开始新的HTTP连接(1):www.google.com[2012-12-19 18:49:58,594:INFO/MainProcess]开始新的HTTP连接(1):www.google.ca[2012-12-19 18:49:58,801:INFO/MainProcess]任务task.geturl [48182400-7d67-466b-ba5c-867747cb3974]成功实现0.402245998383s:无

[2012-12-19 18:49:58,400: INFO/MainProcess] Starting new HTTP connection (1): www.google.com [2012-12-19 18:49:58,594: INFO/MainProcess] Starting new HTTP connection (1): www.google.ca [2012-12-19 18:49:58,801: INFO/MainProcess] Task tasks.geturl[48182400-7d67-466b-ba5c-867747cb3974] succeeded in 0.402245998383s: None

但是,当我通过调用geturl('http://www.google.com')以同步方式运行此程序时,会收到响应.我已经阅读了文档,似乎无法了解为什么这行不通.此方法的主要用途是轮询API,请有人解释为什么这不起作用?

But when I run this in a synchronous fashion by calling geturl('http://www.google.com'), I get a response. I have read through the docs and cannot seem to clue into why this is not working. The primary use of this is to poll API's could someone please explain why this does not work?

谢谢!

推荐答案

res.content 只是一个 str 实例,没有理由您无法把它返还.您的问题可能出在其他地方,可能在您的芹菜配置中.

res.content is just an str instance, there's no reason you wouldn't be able to return it. Your issue likely lies elsewhere, probably in your celery configuration.

许多配置参数在结果显示方式上都起着作用已存储;看一看.
您要检查的第一个可能是 CELERY_IGNORE_RESULT .

您还应该检查结果代理配置.

Your should also check your result broker configuration.

这篇关于我可以在celery上使用Python请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆