zoukankan      html  css  js  c++  java
  • Python学习---IO的异步[asyncio +aiohttp模块]

    aiohttp

    aiohttp是在asyncio模块基础上封装的一个支持HTTP请求的模块,内容比8.4.2【基于asyncio实现利用TCP模拟HTTP请求】更全面

    安装aiohttp:

    pip3 install aiohttp

    asyncio + aiohttp实现异步请求【有问题】

    import aiohttp
    import asyncio
    @asyncio.coroutine
    def fetch_async(url):
        print(url)
        response = yield from aiohttp.request('GET', url)
        # data = yield from response.read()
        # print(url, data)
        print(url, response)
        response.close()
    tasks = [fetch_async('http://www.cnblogs.com/'), fetch_async('http://www.chouti.com/')]
    event_loop = asyncio.get_event_loop()
    results = event_loop.run_until_complete(asyncio.gather(*tasks))
    event_loop.close()

    image

    asyncio + requests完成IO异步

    asyncio + requests完成IO异步

    import asyncio
    import requests
    @asyncio.coroutine
    def fetch_async(func, *args): 
        # 获取事件循环:就是有个循环一直等待这用户的响应
        loop = asyncio.get_event_loop()
        future = loop.run_in_executor(None, func, *args) # 执行传递进来的get函数
        response = yield from future
        print(response.url, response.content)
    tasks = [
        fetch_async(requests.get, 'http://www.cnblogs.com/ftl1012/'),
        fetch_async(requests.get, 'http://dig.chouti.com/images/homepage_download.png')
    ]
    loop = asyncio.get_event_loop()
    results = loop.run_until_complete(asyncio.gather(*tasks))
    loop.close()

    image

  • 相关阅读:
    《网络攻防实践》6.0
    《网络攻防实践》5.0
    Docker 本地镜像发布到阿里云(完结篇)
    Vue 实战-9 Vue公共js功能函数的封装和使用
    Vue 实战-8 单独运行测试.js文件
    Docker 常用安装
    DockerFile 解析及案例
    Docker 容器数据卷
    Docker 镜像原理
    多字段模糊匹配 -->搜索功能(mysql原生语句实现)
  • 原文地址:https://www.cnblogs.com/ftl1012/p/9424792.html
Copyright © 2011-2022 走看看