1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import asyncio import time from concurrent.futures.thread import ThreadPoolExecutor
import aiohttp
async def fetch(url): async with aiohttp.ClientSession() as session: async with session.get(url) as response: return await response.text()
async def baidu1(): url = 'https://www.baidu.com' content = await fetch(url) print(f"baidu 1")
async def baidu2(): url = 'https://www.baidu.com' content = await fetch(url) print(f"baidu 2")
async def deal(): tasks = [baidu1(), baidu2()] await asyncio.gather(*tasks)
def test(): asyncio.run(deal())
if __name__ == '__main__': pool = ThreadPoolExecutor(max_workers=12) pool.submit(test) pool.submit(test) time.sleep(10)
|