aiohttp使用方法
import aiohttp
async def test_aiohttp():
async with aiohttp.ClientSession() as session:
#http://127.0.0.1:8013/get_test_info我自己本地开的一个web服务
async with session.post('http://127.0.0.1:8013/get_test_info', ) as resposne:
print(resposne.url)
print('prime_filter end')
return 'prime_filter end'
def get_result(x):
print("result")
print(x.result())
def main():
"""主函数"""
start_time = time.time()
loop = asyncio.get_event_loop()
future = asyncio.gather(test_aiohttp(), test_aiohttp()
,test_aiohttp()
,test_aiohttp()
,test_aiohttp()
,test_aiohttp())
future.add_done_callback(get_result)
loop.run_until_complete(future)
loop.close()
print(time.time()-start_time)
if __name__ == '__main__':
main()
这个并发量与web服务器也有关系,我本地测试我的web服务器可以一次处理九个请求.多余九个后就会阻塞.