程序的执行:
串行执行:
import requests
import time
def get_page(url):
print('GET: %s' %url)
response=requests.get(url)
print(url,len(response.text))
return response.text
urls=[
'https://www.baidu.com',
'https://www.python.org',
'https://www.openstack.org'
]
#程序的执行是串行执行
start=time.time()
for url in urls:
res=get_page(url) #任务的调用方式是同步调用:提交一个任务,然后在原地等待,等待返回结果后再执行下一行代码
stop=time.time()
print(stop-start) #2.664152145385742
串行不以为效率一定低,当程序是纯计算的话,串行执行并没有效率问题.
但是如果遇到IO密集型程序串行使,效率极低.
所以需要进行 并发
并发执行(并行执行):
from multiprocessing import Process
from threading import Thread
import