2016-05-09 19 views
5

Falcon web framework'ü gevents ve asyncio gibi async çalışanları ile kullanmaya çalışıyorum. Öğreticiler için etrafa baktım, ama ben falcon ile gevent uygulamasını birleştiren herhangi birini bulamadım. Daha önce hiç kullanılmamış olduğum için, bu kombinasyonu nasıl test edeceğimi bilmiyorum. Birisi bana bir örnek veya öğretici için rehberlik edebilir mi?Gevents Falcon ile nasıl kullanılır?

Teşekkür ederiz!

cevap

3

Sadece geçmişte yaptığım bir şeyle Falcon ve gevent ile yeni bir web sitesi inşa etmek istiyordum. Bunun hakkında garip bir şey olduğunu biliyordum, bu yüzden internette araştırdım ve soruyu buldum. Henüz kimsenin cevap vermediğine şaşırdım. Yani, benim daha önceki kod bir göz için geri gitti ve şu yukarı ve (a çok hızlı çerçevesinde yapar) Falcon ve gevent ile kullanmaya başlamanız için temel iskelet: Size şöyle

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 

Görebilir, büyük hile maymun-yama içinde. Bunun dışında, gerçekten çok basit. Umarım bu birisine yardım eder!

+1

Bu kurulum benim için harika çalıştı. Sadece Falcon'un kendisi ile birlikte çalışabileceğini değil, aynı zamanda uygulamanızdaki “spawn”, “sleep” ve “Semaphore” gibi yapıların avantajlarından faydalanabileceğinizi de unutmayın. Talep odaklı koddan bağımsız olarak çalışan arka plan çalışanları oluşturmaları için onları kullandım. – killthrush

+1

Ayrıca, Gevent-etkin pywsgi'nin aksine Bjoern sunucusunu kullanarak Falcon'ı çalıştırmayı denedim. Birincisi daha hızlıdır ve benim merhaba dünya testlerimin bazılarına dayanarak daha yüksek süren istekleri/sn'yi destekleyebilir, ancak etkinlik döngüsü opaktır ve uygulama kodunuzda gevent kullanamazsınız. @ kvaruni'nin kurulumu, gevent olay döngüsüne girmenizi sağlar. Bu, Falcon'ı barındırmak için bir WSGI sunucusu seçerken dikkat etmeniz gereken bir şey. – killthrush

İlgili konular