2017-08-02 29 views
0

我想通過簡單的方式與​​同時執行100個請求。龍捲風產量[<期貨清單>]同時只執行10個請求

但是這個方法當時只能執行10個請求!

我準備了一個簡短的例子,它可以實現它,只要運行,你就會看到當時10個請求的一部分執行的請求。

用debian stretch和ubuntu 16.04測試,結果相同。

的Python 3.6.1,
龍捲風== 4.5.1

from datetime import datetime 
import tornado.ioloop 
import tornado.gen 
import tornado.web 
from tornado.httpclient import AsyncHTTPClient 


# the same for tornado and curl clients 
# AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient') 
http_client = AsyncHTTPClient() 


class MainHandler(tornado.web.RequestHandler): 

    @tornado.gen.coroutine 
    def get(self, **kwargs): 
     yield self.write('<html><pre>') 
     yield tornado.gen.sleep(5) 
     yield self.finish('long page test</pre></html>') 


def make_app(): 
    return tornado.web.Application([ 
     tornado.web.url('^/test', MainHandler), 
    ]) 


@tornado.gen.coroutine 
def long_request(n): 
    print('long_request {n} start'.format(n=n)) 

    response = yield http_client.fetch('http://127.0.0.1:8000/test') 
    yield tornado.gen.sleep(5) 

    print('{date} long_request {n} finish, size {size}'.format(
     date=datetime.now(), n=n, size=len(response.body)) 
    ) 


@tornado.gen.coroutine 
def requests_handler(): 
    print('Requests handler started') 
    yield [long_request(n) for n in range(100)] 
    print('Requests handler finished') 


app = make_app() 
app.listen(8000, '127.0.0.1') 

tornado.ioloop.IOLoop.current().add_callback(callback=requests_handler) 
tornado.ioloop.IOLoop.current().start() 

回答

0

哎呀,才發現那是什麼。

每個客戶端只能執行max_clients請求。

AsyncHTTPClient.configure(
    'tornado.curl_httpclient.CurlAsyncHTTPClient', 
    max_clients=100 
) 

AsyncHTTPClient.configure(
    'tornado.simple_httpclient.SimpleAsyncHTTPClient', 
    max_clients=100 
) 

但我想這不是一個明顯的行爲,所以我認爲它應該留在這裏爲下一個googlers。