HTTPClient 使用例子:
 
            from tornado.httpclient import HTTPClient 
 
 def synchronous_fetch(url): 
     http_client = HTTPClient() 
     response = http_client.fetch(url) 
     return response.body
 
        AsyncHTTPClient使用例子:
方法1:
    from tornado.httpclient import AsyncHTTPClient 
 def asynchronous_fetch(url, callback): 
  http_client = AsyncHTTPClient() 
 def handle_response(response): # 創建一個函數內的函數,來處理返回的結果
 callback(response.body) 
 http_client.fetch(url, callback=handle_response)   # 異步處理結束后會調用指定的callback的函數
方法2:
    from tornado.httpclient import AsyncHTTPClient 
    from tornado.concurrent import Future 
 def async_fetch_future(url): 
 http_client = AsyncHTTPClient() 
 my_future = Future() 
 fetch_future = http_client.fetch(url) 
 fetch_future.add_done_callback(lambda f: my_future.set_result(f.result())) 
 return my_future
 
        
方法3:
    from tornado.httpclient import AsyncHTTPClient 
    from tornado import gen 
 @gen.coroutine #添加異步訪問的裝飾器
 def fetch_coroutine(url): 
 http_client = AsyncHTTPClient() 
 response = yield http_client.fetch(url) # 獲取異步結果時要使用yield 
 raise gen.Return(response.body)  # 使用拋出異常的方式來返回結果,不能使用return來返回
以下知識是額外的可以了解下,但不保證知識是完整的:
  async and await 在python3.5,tornado4.3中可以了解下 
 
  例子:
  async deffetch_coroutine(url): 
   http_client = AsyncHTTPClient()
 response = await http_client.fetch(url) 
 return response.body
 
使用yield來遍歷異步的結果是,以下方法是在項目中沒有試驗過的
--------------------------------- start --------------------------------------
 
         
         
        @gen.coroutine
def parallel_fetch(url1, url2): resp1, resp2 = yield [http_client.fetch(url1), http_client.fetch(url2)] @gen.coroutine def parallel_fetch_many(urls): responses = yield [http_client.fetch(url) for url in urls] # responses is a list of HTTPResponses in the same order @gen.coroutine def parallel_fetch_dict(urls): responses = yield {url: http_client.fetch(url) for url in urls} # responses is a dict {url: HTTPResponse}
--------------------------------- end ----------------------------------------