这段代码,每次循环都是start一个线程,并等待结束后再开始下一个线程。
for t in processes:
t.start()
t.join()
改为:
for t in processes:
t.start()
for t in processes:
t.join()
完整的测试代码:
import time
import multiprocessing
from datetime import datetime
def mainFunction(idx,running):
print('thread {0} is running.'.format(idx))
while(running.value):
pass
print('thread {0} is closing .'.format(idx))
if __name__ == '__main__':
running = multiprocessing.Value('i',1)
print('Started at ', datetime.now())
processes = []
for i in range(0,10):
t = multiprocessing.Process(target = mainFunction, args = (i,running,))
processes.append(t)
for t in processes:
t.start()
time.sleep(10)
running.value = 0
运行结果:
('Started at ', datetime.datetime(2020, 5, 14, 11, 9, 47, 862220))
thread 0 is running.
thread 1 is running.
thread 2 is running.
thread 3 is running.
thread 4 is running.
thread 5 is running.
thread 7 is running.
thread 6 is running.
thread 9 is running.
thread 8 is running.
thread 1 is closing.
thread 7 is closing.
thread 2 is closing.
thread 3 is closing.
thread 0 is closing.
thread 5 is closing.
thread 4 is closing.
thread 9 is closing.
thread 6 is closing.
thread 8 is closing.
('Finished at ', datetime.datetime(2020, 5, 14, 11, 9, 57, 989041))