ClientSession中进行tcp连接的问题

时间:2018-04-24 02:15:53

标签: python concurrency connection-pooling scalability python-asyncio

我正在使用asyncio对我的服务器进行异步api调用,这是一个虚拟服务器,只返回ack。

import aiohttp
import time
import asyncio
import uvloop

asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

def _get_session_headers() -> dict:
    return {}

start=time.time()
conn = aiohttp.TCPConnector(limit=10240)
_session = aiohttp.ClientSession(connector=conn,headers=_get_session_headers())


async def test(data, value): 
    async with _session.request('POST', "http://target:8080/path", data=data) as response:
        buffer = await response.read()
        return buffer


data = initialize_data()

async def run():
    tasks = []
    start = time.time()
    for i in range(10000):
        tasks.append(asyncio.ensure_future(test(data, i)))
    end = time.time() 
    print(end-start)
    responses = asyncio.gather(*tasks)
    await responses

start = time.time()
ioloop = asyncio.get_event_loop()
ioloop.run_until_complete(run())
end = time.time()
print(end-start)

task=ioloop.create_task(_session.close())
ioloop.run_until_complete(task)
ioloop.close()

我已将打开文件的数量设置为10240,这样我就没有太多文件打开问题。所以我尝试进行10K并发连接,大约需要10秒。我想弄清楚瓶颈在哪里

有一点,我意识到在我启动脚本后,建立tcp连接大约需要5-6秒。

sudo lsof -i -a -p `ps aux | grep python | grep myservice | awk -F " " '{print $2}'` | wc -l

我尝试在运行程序后立即检查我的进程所做的tcp连接数。但是要显示连接大约需要5-6秒。我的意思是上面的命令在前5-6秒显示0。所以我想知道为什么这么长时间。之后,脚本可以进行10K tcp连接并完成。

我尝试将网址更改为http://google.com而不是http://target:8080/path,但是仍然需要大约5-6秒才能显示任何tcp连接。我在想为什么这么长时间。有什么建议? 同样是AFAIK,ClientSession也有连接池,因此应该更快。我是否正确使用ClientSession,我的意思是有一种方法可以正确初始化它,以便tcp连接更快

这是我运行的cprofile

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        2    1.076    0.538   12.594    6.297 {method 'run_until_complete' of 'uvloop.loop.Loop' objects}
    10664    0.850    0.000    0.937    0.000 {method 'feed_data' of 'aiohttp._http_parser.HttpParser' objects}
    25000    0.836    0.000    1.088    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/connector.py:796(_wrap_create_connection)
    10664    0.719    0.000    1.722    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/client_proto.py:140(data_received)
    10000    0.397    0.000    0.402    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/client.py:176(request)
    40000    0.345    0.000    8.552    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/client.py:180(_request)
    60000    0.313    0.000    1.157    0.000 /home/me/venv2/lib/python3.6/site-packages/idna/core.py:231(check_label)
    19998    0.311    0.000    0.371    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/locks.py:22(wait)
    20000    0.265    0.000    0.265    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/client.py:635(_prepare_headers)
    10000    0.262    0.000    0.262    0.000 {method 'copy' of 'multidict._multidict.CIMultiDict' objects}
   270000    0.240    0.000    0.478    0.000 /home/me/venv2/lib/python3.6/site-packages/idna/intranges.py:38(intranges_contain)
    40143    0.147    0.000    9.338    0.000 grpc.py:31(test)
    20000    0.145    0.000    0.178    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/streams.py:528(read)
    10000    0.142    0.000    0.436    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/client_reqrep.py:477(send)
522950/522949    0.140    0.000    0.207    0.000 {built-in method builtins.isinstance}
    10000    0.133    0.000    0.212    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/client_reqrep.py:275(update_headers)
    20000    0.132    0.000    0.594    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/client_reqrep.py:679(start)
    30000    0.126    0.000    1.885    0.000 /home/me/venv2/lib/python3.6/site-packages/aiohttp/connector.py:809(_create_direct_connection)
    60000    0.125    0.000    0.160    0.000 /home/me/venv2/lib/python3.6/site-packages/idna/core.py:63(check_bidi)
   270000    0.120    0.000    0.120    0.000 {built-in method _bisect.bisect_left}

任何可能出错的想法?​​

0 个答案:

没有答案