提高速度"发送100,000个请求"通过asyncio使用多进程和多线程

时间:2016-11-02 17:04:30

标签: python multithreading python-asyncio aiohttp

首先,我想尽快使用1个连接发送多个请求。下面的代码运行良好而快速,但我希望它超越异步。回到我的问题,是否可以使用多线程或多处理并行运行它。我听说你可以使用ThreadPoolExecutor或ProcessPoolExecutor。

import random
import asyncio
from aiohttp import ClientSession
import time
from concurrent.futures import ProcessPoolExecutor

async def fetch(sem,url, session):
    async with sem:
        async with session.get(url) as response:
            return await response.read()
async def run(r):
    url = "http://www.example.com/"
    tasks = []
    sem = asyncio.Semaphore(1000)
    async with ClientSession() as session:
        for i in range(r):
            task = asyncio.ensure_future(fetch(sem, url.format(i), session)) #return a task
            tasks.append(task)
        responses = asyncio.gather(*tasks)
        await responses
if __name__ == "__main__":
    number = 10000
    loop = asyncio.get_event_loop()
    start = time.time()
    loop.run_until_complete(run(number))
    end = time.time() - start
    print (end)

从测试开始,它设法在49秒内发送了大约10k的请求。 我需要它更快,有什么建议吗? (线程,过程)

1 个答案:

答案 0 :(得分:1)

ProcessPoolExecutor是一种进行真正的多处理的方法。 对于您的用例,它基本上就像您同时启动程序的多个副本一样。如果您的计算机需要带宽和CPU,则应该能够通过使用ProcessPoolExecutor(max_workers = 4)将性能提高4(

但是每个子进程都需要一个asyncio事件循环,所以你可以这样做:

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


with concurrent.futures.ProcessPoolExecutor(max_workers=4) as exc:
    exc.submit(main, 2500)
    exc.submit(main, 2500)
    exc.submit(main, 2500)
    exc.submit(main, 2500)

作为run函数的旁注:您也无需使用ensure_future或任务,async def函数的结果是协程,您可以直接使用等待或传递给asyncio.gather

async def run(r):
    url = "http://www.example.com/"
    sem = asyncio.Semaphore(1000)
    async with ClientSession() as session:
        coros = [fetch(sem, url.format(i), session) for i in range(r)]
        await asyncio.gather(*coros)
相关问题