2016-05-09 55 views
5

Sto provando a utilizzare il framework web Falcon con operatori asincroni come gevents e asyncio. Sono stato in cerca di tutorial, ma non sono riuscito a trovare nessuno che combini l'implementazione di gevent con falcon. Dal momento che non ho mai usato i gevents in precedenza, non sono sicuro di come testare questa combinazione. Qualcuno può guidarmi ad un esempio o ad un tutorial?Come usare Gevents con Falcon?

Grazie! :)

risposta

3

Stavo solo cercando di costruire un nuovo sito con Falcon e Gevent, qualcosa che avevo fatto in passato. Sapevo che c'era qualcosa di strano in questo, quindi ho cercato online e ho trovato la tua domanda. Sono piuttosto sorpreso che nessuno abbia ancora risposto. Così, sono tornato a dare un'occhiata al mio codice prima e il seguente è lo scheletro di base per ottenere installato e funzionante con Falcon e gevent (che rende per un quadro molto veloce):

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 

Come si può vedere, il grande trucco è nella patch-scimmia. Oltre a questo, è davvero semplice. Spero che questo aiuti qualcuno!

+1

Questa configurazione ha funzionato bene per me. Vale la pena notare che non solo lo stesso Falcon può funzionare con gevents, ma puoi anche sfruttare i costrutti di gevents come 'spawn',' sleep' e 'Semaphore' nella tua app. Li ho usati per creare background worker che funzionavano indipendentemente dal codice basato su richiesta. – killthrush

+1

Ho anche provato ad eseguire Falcon usando il server Bjoern invece del pywsgi abilitato a gevent. Il primo è più veloce e può supportare richieste/sec prolungate sostenute in base ad alcuni dei miei test hello-world, ma il ciclo degli eventi è opaco e non è possibile utilizzare gevent nel codice dell'app. La configurazione di @kvaruni ti consente di accedere al ciclo di eventi gevent. Questo è certamente qualcosa da considerare quando si seleziona un server WSGI per ospitare Falcon. – killthrush