2015-10-29 33 views
0

我正在使用python發送多個HTTP請求..> 1000個請求在同一時間被異步觸發。但我得到的錯誤:max_clients限制達到,請求排隊龍捲風和過了一段時間我越來越超時錯誤。已達到max_clients限制,請求排隊龍捲風

如何解決這類問題,發送多個http post請求並避免超時?

這裏是我使用的代碼:

class AjaxBatchHandler(basehandler.BaseHandler): 
    @tornado.gen.coroutine 
    def post(self): 
     # just respond with a status, no redirect to login 
     if not self.get_current_user: 
      self.set_status(403) 

     batch = json.loads(self.get_argument("actions").encode('utf-8')) 

     client = tornado.httpclient.AsyncHTTPClient() 

     batch_requests = [] 
     for item in batch['actions']: 
      request = utils.build_request(
       self, 
       action=item['action'].replace("{API}", utils.get_api_baseurl()), 
       values=item['values']) 
      batch_requests.append(client.fetch(request)) 

     try: 
      batch_responses = yield batch_requests 

      batch_result = dict(results=[]) 
      for result in batch_responses: 
       batch_result['results'].append(json.loads(result.body)) 

     except tornado.httpclient.HTTPError as e: 
      batch_result = dict(results=[]) 
      batch_result['results'].append({"Status": 500, 
             "StatusMsg": e.message, 
             "Error": e.code 
             }) 

     self.write(batch_result) 

回答

2

要麼增加max_clients限制(如果它是適合您要發送更多的流量到你打的網站)或您的要求減慢。對於前者,請在您的程序開始時執行

AsyncHTTPClient.configure(None, max_clients=1000) 

。對於後者,信號量或隊列可用於控制發送請求的速率。請參閱https://github.com/tornadoweb/tornado/blob/master/demos/webspider/webspider.py

+0

通過發送大量的異步調用一次,很多我的意思是10.000例如,比增加max_clients ist沒有解決問題。 但我會仔細看看隊列。但是我現在解決了這個問題。我使用ajax調用將每個調用異步發送,並收集響應,當所有響應都在那裏時,我顯示結果。我注意到這是一個較慢的過程,但沒有超時工作。 – Monicka