[英]Tornado Blocking Code
我只是想到了龍卷風和事件驅動編程的非阻塞基礎結構。 實際上,我正在寫一個簡單的Web應用程序,它正在訪問外部Web服務的HTTP-API。 我了解為什么我應該將此API稱為非阻塞。 但是,如果僅執行第一個調用非阻塞操作,則IOLoop可以進一步循環,是否有任何缺點?
例如:
@tornado.web.asynchronous
def get(self):
nonblocking_call1(self._callback)
def _callback(self, response):
self.write(str(response))
self.write(str(blocking_call2()))
self.write(str(blocking_call3()))
self.finish()
與
@tornado.web.asynchronous
def get(self):
nonblocking_call1(self._nonblocking_callback1)
def _callback1(self, response):
self.write(str(response))
nonblocking_call2(self._nonblocking_callback2)
def _callback2(self, response):
self.write(str(response))
nonblocking_call3(self._nonblocking_callback3)
def _callback3(self, response):
self.write(str(response))
self.finish()
如果在龍卷風內部使用阻止代碼,則在等待任何阻止代碼的情況下,同一龍卷風進程無法處理任何其他請求。 您的應用程序將不支持多個用戶同時使用,即使阻塞調用僅花費100毫秒左右的時間,它仍將是巨大的性能殺手。
如果寫這種方法對您來說很累(對我來說),則可以使用龍卷風的gen
模塊:
class GenAsyncHandler(RequestHandler):
@asynchronous
@gen.engine
def get(self):
http_client = AsyncHTTPClient()
response = yield gen.Task(http_client.fetch, "http://example.com")
do_something_with_response(response)
self.render("template.html")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.