我正在尝试将 Falcon Web 框架与 gevents 和 asyncio 等异步工作者一起使用。我一直在寻找教程,但我找不到任何将 gevent 的实现与 falcon 结合起来的教程。由于我以前从未使用过 gevents,因此我不确定如何测试这种组合。有人可以指导我查看示例或教程吗?
谢谢!:)
我正在尝试将 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
如您所见,最大的窍门在于猴子修补。除此之外,它真的很简单。希望这对某人有帮助!