IO操作首选多线程
计算操作用别的首先实现并发较好:multiprocessing模块
充分运用多核,多CPU的计算能力,适用于计算密集型任务
import time
import multiprocessing
def func(n):
print(f'{multiprocessing.current_process().name}执行开始于:{time.ctime()}')
time.sleep(n)
print(f'{multiprocessing.current_process().name}执行开始于:{time.ctime()}')
def main ():
print(f'主函数运行于:{time.ctime()}')
processes = []
p1 = multiprocessing.Process(target = func, args = (4,))
processes.append(p1)
p2 = multiprocessing.Process(target = func, args = (2,))
processes.append(p2)
for p in processes:
p.start()
for p in procsses:
p.join()
print(f'主函数结束于:{time.ctime()}')
if __name__ =='__main__':
main()