2015-09-03 76 views
1
def downloadTile(root, message): 
    data = message 
    tornado.httpclient.AsyncHTTPClient.configure(None, max_clients=2000) 
    http_client = AsyncHTTPClient() 
    http_client.fetch(data.url, functools.partial(handle_request, root, message)) 


def handle_request(root, message, response): 
    '''callback needed when a response arrive''' 
    data = message 
    dirpath = os.path.join(root, str(data.z), str(data.x)) 
    if not os.path.exists(dirpath): 
    os.makedirs(dirpath) 
    # filepath 
    filepath = os.path.join(dirpath, '%s.png' % data.y) 
    with open(filepath, "w") as f: 
     f.write(str(response.body)) 
     log.debug("Downloading Progressing") 

Fetch返回未來,並且不會等待下載完成。有了這段代碼,我們實際上幾乎在同一時間發送每一個請求。這可能是爲什麼下載失敗的一些瓷磚。如何批量處理龍捲風http_client.fetch

如何處理小批量或連續讀取?

回答

0

最簡單的解決方案是不要設置max_clients這麼高。默認值是10;在大多數情況下將其設置爲2000是過度和不適當的。超過max_client限制的任何請求將排隊等待,直到另一個請求結束。

+0

似乎工作,但仍然不是100%@Ben達內爾 –

+0

見https://github.com/tornadoweb/tornado/blob/88eefe943461a354165db5398602d1185c6656b9/demos/webspider/webspider.py使用隊列流量控制更完整的示例。 –