How to use Gevents with Falcon?

I am trying to use the Falcon web framework with asynchronous workers like gevents and asyncio. I was looking for tutorials, but I could not find any combining the implementation of gevent with falcon. Since I have never used gevents before, I'm not sure how to get tested with this combination. Can someone lead me to an example or a textbook?

Thanks!:)

+5
source share
1 answer

I just wanted to create a new site with Falcon and gevent, which I did in the past. I knew there was something strange about this, so I searched the Internet and found your question. I am somewhat surprised that no one answered. So, I came back to look at my earlier code, and the following basic skeleton, to get up and work with Falcon and gevent (which makes it very fast):

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(("", port), api) # address and port to bind to ("" is localhost), and the Falcon handler API server.serve_forever() # once the server is created, let it serve forever 

As you can see, the big trick is in the monkey patch. Other than that, it's really quite simple. Hope this helps someone!

+3
source

All Articles