async/await
python3.5又加入了async/await特性.
异步处理比同步处理能提升成百上千倍的效率,弥补了python由于GIL(全局锁)的存在,不能发挥多核的优势性能方面的短板.
普通 io
import time
import asyncio
def __my_fun(future, para:int):
print('para is: {}'.format(para))
result = para+1
future.set_result(result)
async def my_fun(para):
loop = asyncio.get_event_loop()
future = loop.create_future()
loop.call_soon(__my_fun, future, para)
result = await future
return result
async def hello(para, result:list):
one_result = await my_fun(para)
result.append(one_result)
print('Hello World:%s' % time.time())
if __name__ =='__main__':
loop = asyncio.get_event_loop()
result = []
tasks = [hello(i, result) for i in range(5)]
loop.run_until_complete(asyncio.wait(tasks))
print(result)
aiohttp
import time
import asyncio
from aiohttp import ClientSession
tasks = []
url = "https://www.baidu.com/{}"
# url paramater
async def hello(url):
async with ClientSession() as session:
async with session.get(url) as response:
print('Hello World:%s' % time.time())
return await response.read()
def run():
for i in range(5):
# future can return result
task = asyncio.ensure_future(hello(url.format(i)))
tasks.append(task)
# get result
result = loop.run_until_complete(asyncio.gather(*tasks))
print(result)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
run()
限制并发量
#coding:utf-8
import time,asyncio,aiohttp
url = 'https://www.baidu.com/'
async def hello(url,semaphore):
async with semaphore:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.read()
async def run():
semaphore = asyncio.Semaphore(500) # 限制并发量为500
to_get = [hello(url.format(),semaphore) for _ in range(1000)] #总共1000任务
await asyncio.wait(to_get)
if __name__ == '__main__':
# now=lambda :time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
loop.close()