gpt4 book ai didi

python - 如何将 Gevents 与 Falcon 一起使用?

转载 作者:太空狗 更新时间:2023-10-29 20:53:38 25 4
gpt4 key购买 nike

我正在尝试将 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

如您所见,关键在于猴子修补。除此之外,它真的很简单。希望这对某人有帮助!

关于python - 如何将 Gevents 与 Falcon 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37119047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com