2017-04-23 17 views
0

현재 10 초당 3000 건의 요청을 처리하는 API를 사용하고 있습니다. 나는 비동기 IO 성격 때문에 토네이도를 사용하여 가져온 10,000 개의 URL을 가지고있다.Python 토네이도 속도 제한 AsyncHttpClient 가져 오기

API 한도를 반영하여 요금 제한을 구현하려면 어떻게해야합니까?

from tornado import ioloop, httpclient 

i = 0 

def handle_request(response): 
    print(response.code) 
    global i 
    i -= 1 
    if i == 0: 
     ioloop.IOLoop.instance().stop() 

http_client = httpclient.AsyncHTTPClient() 
for url in open('urls.txt'): 
    i += 1 
    http_client.fetch(url.strip(), handle_request, method='HEAD') 
ioloop.IOLoop.instance().start() 

답변

1

i의 값은 3000 요청 간격에 어디에 있는지 확인할 수 있습니다. 예를 들어, i이 3000과 6000 사이에 있으면 6000까지 모든 요청에서 10 초의 시간 초과를 설정할 수 있습니다. 6000 이후에는 시간 제한을 두 배로 늘립니다. 등등.

http_client = AsyncHTTPClient() 

timeout = 10 
interval = 3000 

for url in open('urls.txt'): 
    i += 1 
    if i <= interval: 
     # i is less than 3000 
     # just fetch the request without any timeout 
     http_client.fetch(url.strip(), handle_request, method='GET') 
     continue # skip the rest of the loop 

    if i % interval == 1: 
     # i is now 3001, or 6001, or so on ... 
     timeout += timeout # double the timeout for next 3000 calls 

    loop = ioloop.IOLoop.current() 
    loop.call_later(timeout, callback=functools.partial(http_client.fetch, url.strip(), handle_request, method='GET')) 

: 나는 단지 요청 적은 수의이 코드를 테스트했다. i의 값은 함수에서 i을 뺀 것이므로 변경 될 수 있습니다. 그렇다면 i과 비슷한 다른 변수를 유지하고 그 변수에 대해 뺄셈을 수행해야합니다.