I am trying to use Falcon web framework with async workers like gevents and asyncio. I have been looking around for tutorials, but I haven't been able to find any which combine implementation of gevent with falcon. Since I have never used gevents before, I am not sure how to go around testing this combination. Can someone guide me to an example or a tutorial?
Thank you! :)
I was just looking to build a new website with Falcon and gevent, something I had done in the past. I knew that there was something odd about it, so I searched online and found your question. I'm somewhat surprised no one has responded yet. So, I went back to have a look at my earlier code and the following is the basic skeleton to get up and running with Falcon and gevent (which makes for a very fast framework):
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
As you can see, the big trick is in the monkey-patching. Other than that, it really is quite straightforward. Hope this helps someone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With