只有本人能看懂的-Python线程,进程,携程,I/O同步,异步
举个栗子:
我想get三个url,先用普通的for循环
import requests
from multiprocessing import Process
from threading import Thread
import requests
import time
# -----正常遍历 串行 同步-----
def get_page(url):
page = requests.get(url)
print(url)
start = time.time()
urls = ['http://jandan.net/','https://www.python.org','http://www.gamersky.com/']
for i in urls:
get_page(i)
end = time.time()
print(end - start)
# 这是正常的
这回我用threading模块给他弄快点。
def get_page(url):
page = requests.get(url)
start = time.time()
list = [] # 线程对象列表
urls = ['http://jandan.net/','http://www.xiaohuar.com/','http://www.gamersky.com/']
for i in urls:
ok = Thread(target=get_page,args=(i,)) # target为函数名,args为给函数传的参数
list.append(ok)
# get_page(i)
for i in list:
i.start() # 启动一下线程
for i in list:
i.join() # 主线程等待子线程执行之后结束
end = time.time()
print(end - start)
# 开了仨线程运行
# 如果线程使用了join()函数(主线程代码将停在join) ,主进程将等待子线程执行结束再执行
# 如果子线程(ok.setDeamon(True))变成守护线程,那么主线程将不会等待子线程执行结束,当主线程结束时,不管子线程是否执行完毕都将强制终止!
我再瞅瞅进程
import multiprocessing
import time
def run():
i = 0
while i <10000:
time.sleep(2)
print(i)
i+=1
if __name__ == "__main__":
p = multiprocessing.Process(target=run) # 一个进程
p.start()
print(p.pid) # 查看进程的pid
再来个多进程
import multiprocessing
import time
def run():
i = 0
while i <10000:
time.sleep(2)
print(i)
i+=1
if __name__ == "__main__":
p = multiprocessing.Process(target=run) # 一个进程
p1 = multiprocessing.Process(target=run) # 一个进程
p2 = multiprocessing.Process(target=run) # 一个进程
p.start()
p1.start()
p2.start()
print(p.pid) # 查看进程的pid