每天进步一点点-多进程和多线程以及内置队列使用
多进程
import multiprocessing as mp
import threading as td
import time
def job(q):
res = 0
for i in range(10000000):
res += i + i ** 2 + i ** 3
q.put(res) # queue
def multicore():
q = mp.Queue()
p1 = mp.Process(target=job, args=(q,))
p2 = mp.Process(target=job, args=(q,))
p1.start()
p2.start()
p1.join()
p2.join()
res1 = q.get()
res2 = q.get()
print('multicore:', res1 + res2)
def normal():
res = 0
for _ in range(2): # 线程或进程都构造了两个,进行了两次运算,所以这里循环两次
for i in range(10000000):
res += i + i ** 2 + i ** 3
print('normal:', res)
def multithread():
q = mp.Queue()
t1 = td.Thread(target=job, args=(q,))
t2 = td.Thread(target=job, args=(q,))
t1.start()
t2.start()
t1.join()
t2.join()
res1 = q.get()
res2 = q.get()
print('multithread:', res1 + res2)
if __name__ == '__main__':
st = time.time()
normal()
st1 = time.time()
print('normal time:', st1 - st)
multithread()
st2 = time.time()
print('multithread time:', st2 - st1)
multicore()
print('multicore time:', time.time() - st2)
''' 运算结果如下
normal: 4999999666666716666660000000
normal time: 20.65348792076111
multithread: 4999999666666716666660000000
multithread time: 20.157374143600464
multicore: 4999999666666716666660000000
multicore time: 13.649165630340576
'''
内置queue
# 因为 线程函数没有返回值,计算的结果无法返回,所以需要一个队列将这个值存放起来,以便后续使用
import threading
import time
from queue import Queue
def job(l,q):
for i in range(len(l)):
l[i] = l[i]**2
q.put(l)
def multithreading():
q = Queue()
threads = []
data = [[1,2,3],[3,4,5],[4,4,4],[5,5,5]]
for i in range(4):
t = threading.Thread(target=job, args=(data[i], q))
t.start()
threads.append(t)
for thread in threads:
thread.join()
results = []
for _ in range(4):
results.append(q.get())
print(results)
if __name__ == '__main__':
multithreading()
# [[1, 4, 9], [9, 16, 25], [16, 16, 16], [25, 25, 25]]
人生苦短,我用python!