import asyncio import random import time # 需求:有一个crontab调度器,每隔1秒,拉起1个job,要求这些job可以并发爬取网页 async def cron_scheduler(): page0 = 1 start = time.time() for i in range(10000): url0 = "{}/{}".format("https://baidu.com", page0) [asyncio.create_task(cron_job(url0)) for i in range(50 )] await asyncio.sleep(0) # 主动让渡线程 end = time.time() print("time:",end-start) async def cron_job(url): n = random.randint(1, 3) # 模拟下载延迟 await asyncio.sleep(n) print("下载结束:", url) asyncio.run(cron_scheduler())
推导式异步更快