2017-08-02 30 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。