1.信号量

image.png

  1. import asyncio
  2. import time
  3. import aiohttp
  4. from main import timethis
  5. semaphore = asyncio.Semaphore(3) # 10并发度
  6. urls = [
  7. f'https://www.cnblogs.com/sitehome/p/{page}'
  8. for page in range(1,10)
  9. ]
  10. async def async_craw(url):
  11. print('start craw url',url)
  12. async with semaphore:
  13. async with aiohttp.ClientSession() as session:
  14. async with session.get(url) as resp:
  15. result = await resp.text()
  16. await asyncio.sleep(5)
  17. print(f'end craw url:{url},time:{len(result)}')
  18. loop = asyncio.get_event_loop()
  19. tasks = [
  20. loop.create_task(async_craw(url))
  21. for url in urls
  22. ]
  23. @timethis
  24. def do_main():
  25. loop.run_until_complete(asyncio.wait(tasks))
  26. if __name__ == '__main__':
  27. do_main()
  28. ###################
  29. do_main 15.98940396308899