import requests
import logging
import time
logging.basicConfig(level=logging.INFO,format='%(asctime)s-%(levelname)s:%(message)s')
TOTAL_NUMBER=100
URL='https://www.httpbin.org/delay/5'
start_time=time.time()
for i in range(1,TOTAL_NUMBER+1):
logging.info('scraping%s',URL)
response=requests.get(URL)
end_time=time.time()
logging.info('total time%s seconds',end_time-start_time)
上面是request遍历程序 直接遍历100遍
下面定义一个协程
import asyncio
async def execute(x):
print('Number:',x)
coroutine=execute(1)
print('Coroutine:',coroutine)
print('After calling execute')
loop=asyncio.get_event_loop()
loop.run_until_complete(coroutine)
print('After calling loop')
结果