首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Gevents与Falcon?

如何使用Gevents与Falcon?
EN

Stack Overflow用户
提问于 2016-05-09 14:59:48
回答 1查看 2.4K关注 0票数 8

我试图使用Falcon web框架与异步工作人员,如gevents和异步。我一直在寻找教程,但我还没有找到任何将gevent的实现与falcon相结合的方法。由于我以前从未使用过gevents,所以我不知道如何测试这个组合。有人能带我去一个例子或一个教程吗?

谢谢!:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-23 13:48:34

我只是想用Falcon和gevent建立一个新的网站,这是我过去做过的事情。我知道这有点奇怪,所以我在网上搜索并找到了你的问题。我有点惊讶至今还没有人回应。因此,我回到前面的代码,下面是使用Falcon和gevent (这是一个非常快速的框架)启动和运行的基本框架:

代码语言:javascript
复制
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

正如你所看到的,最大的诀窍在于猴子的修补。除此之外,它真的很简单。希望这能帮到别人!

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37119047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档