利用 aiohttp 加速网络 IO

import aiohttp
import asyncio
from functools import wraps
import time

baidu = 'http://www.baidu.com'

def func_timer(function):
    '''
    用装饰器实现函数计时
    :param function: 需要计时的函数
    :return: None
    '''
    @wraps(function)
    def function_timer(*args, **kwargs):
        print('[Function: {name} start...]'.format(name = function.__name__))
        t0 = time.time()
        result = function(*args, **kwargs)
        t1 = time.time()
        print('[Function: {name} finished, spent time: {time:.2f}s]'.format(name = function.__name__,time = t1 - t0))
        return result
    return function_timer


async def get(session):
    resp = await session.get(baidu)
    print(await resp.text(encoding='utf-8'))


async def run():
    async with aiohttp.ClientSession() as session:
        for page in range(100):
            await get(session, page)


async def get_(session, queue):
    while True:
        try:
            page = queue.get_nowait()
        except asyncio.QueueEmpty:
            return
        resp = await session.get(baidu)
        print(await resp.text(encoding='utf-8'))


async def run_():
    async with aiohttp.ClientSession() as session:
        queue = asyncio.Queue()
        for page in range(1000):
            queue.put_nowait(page)
        tasks = []
        for _ in range(10):
            task = get(session, queue)
            tasks.append(task)
        await asyncio.wait(tasks)


@func_timer
def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())


@func_timer
def main_():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run_())

运行 main 函数:

if __name__ == '__main__':
    main()

输出:

运行 main_ 函数:

if __name__ == '__main__':
    main_()

输出:

留下评论