协程:是单线程下的并发,又称微线程,纤程。英文名Coroutine。一句话说明什么是协程:协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的。
import asyncio import requests import time asyncio def run(url): prinrt(url) loop= asyncio. get_event_loop() res =await loop. run_in_executor(None,requests. get,url) print(res. url) url_list=["http://www.baidu.com","http://mengsec com"] tasks=[asyncio. ensure_future(run(url)) for url in url_list] loop=asyncio. get_event_loop() loop. run_until_complete(asyncio. wait(tasks))
原文:https://www.cnblogs.com/boye169/p/14815207.html