Python猎鹰和异步操作 [英] Python falcon and async operations
问题描述
我正在使用python3 + falcon组合编写API.
I am writing an API using python3 + falcon combination.
方法中有很多地方可以向客户端发送回复,但是由于执行DB,I/O操作等繁琐的代码,它不得不等到繁重的部分结束.
There are lot of places in methods where I can send a reply to a client but because of some heavy code which does DB, i/o operations, etc it has to wait until the heavy part ends.
例如:
class APIHandler:
def on_get(self, req, resp):
response = "Hello"
#Some heavy code
resp.body(response)
我可以在代码的第一行发送"Hello".我想要的是在后台运行沉重的代码并发送响应,而不管沉重的时间何时结束.
I could send "Hello" at the first line of code. What I want is to run the heavy code in a background and send a response regardless of when the heavy part finishes.
Falcon没有任何内置的异步功能,但是他们提到它可以与gevent之类的东西一起使用.我还没有找到有关如何将两者结合的文档.
Falcon does not have any built-in async capabilities but they mention it can be used with something like gevent. I haven't found any documentation of how to combine those two.
推荐答案
我将Celery用于异步相关的作品.我不知道gevent.请看一下 http://celery.readthedocs.org/en/latest/getting-started/introduction.html
I use Celery for async related works . I don't know about gevent .Take a look at this http://celery.readthedocs.org/en/latest/getting-started/introduction.html
这篇关于Python猎鹰和异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!