2016-08-13 12 views
0

特定のレート(QPS値を使用した例)でAPIリクエストを送信しようとしています。 次のコードを使用してリクエストを開始しています(web_requestsリストに格納されています)。複数のWebリクエストを送信

QPS = 10に設定すると、遅延は0.1秒または100ミリ秒になります。私はtime.sleep(0.1)を使用してリクエストを送信しますが、このコードは30ミリ秒程度のリモートエンドのHTTP応答を待っていますので、0.3秒の余分な遅延が発生します。 応答を待たずに秒間にX Webリクエストを送信できますか?

@gen.coroutine 
def send_requests(campaign_instance): 
    ... 
    http_client = httpclient.AsyncHTTPClient() 
    while True: 
       try:       
        web_request = web_requests.pop() 
        time.sleep(delay) 
        headers = {'Content-Type': 'application/json'} 
        request = httpclient.HTTPRequest(auth_username=settings.api_account, 
                auth_password=settings.api_password, 
                url=settings.api_web_request, 
                body=json.dumps(web_request), 
                headers=headers, 
                request_timeout=5, 
                method="POST") 
        yield http_client.fetch(request, callback=partial(handle_response, web_request["to"])) 

        gen_log.info("start_campaign() Requests in Queue: {}".format(len(web_requests))) 

       except httpclient.HTTPError, exception: 
        gen_log.info.exception("start_campaign() ".format(exception)) 
        api_errors += 1 
        if handle_api_errors(api_errors): 
         break 
       except IndexError: 
        gen_log.info.info('start_campaign() Campaign web requests completed. API Errors: {}'.format(api_errors)) 
        break 

def start(): 
    ioloop.IOLoop.current().run_sync(lambda: send_requests(campaign_instance)) 
    log.info('process_campaign() Campaign completed') 
    campaign_instance.terminate() 

答えて

1

「フェッチ」によって返された未来を単に「収穫」しないでください。その後、コルーチンはすぐにループし続け、フェッチがバックグラウンドで完了するとコールバックが実行されます。

はまた、これまで史上トルネード・アプリケーションの「スリープ」を呼び出すことはありません:

http://www.tornadoweb.org/en/stable/faq.html#why-isn-t-this-example-with-time-sleep-running-in-parallel

その場合は、すべての処理が停止し、あなたの睡眠が完了するまで、「フェッチ」吊り下げられています。代わりに:

yield gen.sleep(delay) 
+0

あなたの助言の後に働いた。ありがとう – spicyramen

関連する問題