如何从芹菜任务中异步调用url

时间:2014-07-06 11:55:55

标签: python asynchronous celery nonblocking

我正在使用芹菜和Tornado,我想知道如何在任务中异步调用url。

我正在寻找以下内容:

@celery.task
def my_task(data):
    def handle_response(response):
        if response.error:
            print "error"
        else:
            print "success"

    http_client = httpclient.AsyncHTTPClient()
    http_client.fetch('some url', handle_response, method='POST', body=data)

或:

@celery.task
@gen.coroutine
def my_task(data):
    http_client = httpclient.AsyncHTTPClient()
    response = yield http_client.fetch('some url', method='POST', body=data)
    raise gen.Result(response.body)

我现在的问题是我没有进入响应处理程序。 使用HttpClient可以工作,但由于它阻止了服务器,我正在寻找一种非阻塞解决方案。

顺便说一句,我的经纪人是Redis,我希望保留它(龙卷风 - 芹菜回调只有在它提供解决方案的时候才能使用pika)

1 个答案:

答案 0 :(得分:4)

在我看来,你的方法是一种开销。 Celery已经被异步执行作业了,那么芹菜任务阻止URL调用的最佳方式是什么呢?在异步任务中添加异步url调用是一种开销。我希望这会有所帮助。