如何将 Gevents 与 Falcon 一起使用?
How to use Gevents with Falcon?
我正在尝试将 Falcon Web 框架与异步工作者(如 gevents 和 asyncio)一起使用。我一直在寻找教程,但我一直没能找到任何将 gevent 的实现与 falcon 结合起来的教程。由于我以前从未使用过 gevents,所以我不确定如何测试这种组合。有人可以指导我查看示例或教程吗?
谢谢! :)
我只是想用 Falcon 和 gevent 建立一个新网站,这是我过去做过的事情。我知道这有些奇怪,所以我在网上搜索并找到了你的问题。我有点惊讶还没有人回应。所以,我回去查看我之前的代码,下面是基本框架,运行 Falcon 和 gevent(这使得 非常快 框架):
from gevent import monkey, pywsgi # import the monkey for some patching as well as the WSGI server
monkey.patch_all() # make sure to do the monkey-patching before loading the falcon package!
import falcon # once the patching is done, we can load the Falcon package
class Handler: # create a basic handler class with methods to deal with HTTP GET, PUT, and DELETE methods
def on_get(self, request, response):
response.status = falcon.HTTP_200
response.content_type = "application/json"
response.body = '{"message": "HTTP GET method used"}'
def on_post(self, request, response):
response.status = falcon.HTTP_404
response.content_type = "application/json"
response.body = '{"message": "POST method is not supported"}'
def on_put(self, request, response):
response.status = falcon.HTTP_200
response.content_type = "application/json"
response.body = '{"message": "HTTP PUT method used"}'
def on_delete(self, request, response):
response.status = falcon.HTTP_200
response.content_type = "application/json"
response.body = '{"message": "HTTP DELETE method used"}'
api = falcon.API()
api.add_route("/", Handler()) # set the handler for dealing with HTTP methods; you may want add_sink for a catch-all
port = 8080
server = pywsgi.WSGIServer(("localhost", port), api) # address and port to bind, and the Falcon handler API
server.serve_forever() # once the server is created, let it serve forever
如您所见,大技巧在于猴子修补。除此之外,它真的很简单。希望这对某人有所帮助!
我正在尝试将 Falcon Web 框架与异步工作者(如 gevents 和 asyncio)一起使用。我一直在寻找教程,但我一直没能找到任何将 gevent 的实现与 falcon 结合起来的教程。由于我以前从未使用过 gevents,所以我不确定如何测试这种组合。有人可以指导我查看示例或教程吗?
谢谢! :)
我只是想用 Falcon 和 gevent 建立一个新网站,这是我过去做过的事情。我知道这有些奇怪,所以我在网上搜索并找到了你的问题。我有点惊讶还没有人回应。所以,我回去查看我之前的代码,下面是基本框架,运行 Falcon 和 gevent(这使得 非常快 框架):
from gevent import monkey, pywsgi # import the monkey for some patching as well as the WSGI server
monkey.patch_all() # make sure to do the monkey-patching before loading the falcon package!
import falcon # once the patching is done, we can load the Falcon package
class Handler: # create a basic handler class with methods to deal with HTTP GET, PUT, and DELETE methods
def on_get(self, request, response):
response.status = falcon.HTTP_200
response.content_type = "application/json"
response.body = '{"message": "HTTP GET method used"}'
def on_post(self, request, response):
response.status = falcon.HTTP_404
response.content_type = "application/json"
response.body = '{"message": "POST method is not supported"}'
def on_put(self, request, response):
response.status = falcon.HTTP_200
response.content_type = "application/json"
response.body = '{"message": "HTTP PUT method used"}'
def on_delete(self, request, response):
response.status = falcon.HTTP_200
response.content_type = "application/json"
response.body = '{"message": "HTTP DELETE method used"}'
api = falcon.API()
api.add_route("/", Handler()) # set the handler for dealing with HTTP methods; you may want add_sink for a catch-all
port = 8080
server = pywsgi.WSGIServer(("localhost", port), api) # address and port to bind, and the Falcon handler API
server.serve_forever() # once the server is created, let it serve forever
如您所见,大技巧在于猴子修补。除此之外,它真的很简单。希望这对某人有所帮助!