aiohttp

作者: 青铜搬砖工 | 来源:发表于2019-07-18 14:41 被阅读0次

    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服务器可以一次处理九个请求.多余九个后就会阻塞.

    相关文章

      网友评论

          本文标题:aiohttp

          本文链接:https://www.haomeiwen.com/subject/aprllctx.html