date: 2019-06-16 22:35:33
author: headsen chen
notice:个人原创
实例代码:
import os,time time.sleep(1) from multiprocessing import Process a = 100 print(a,os.getpid(),os.getppid(),"开始执行") def func(i): print("i = %s,子进程id >>> %s,父进程id >>> %s."%(i,os.getpid(),os.getppid())) if __name__ == "__main__": for i in range(3): p=Process(target=func,args=(i,)) p.start() a += 222 print(a,"并发中") time.sleep(10) print(a,os.getpid(),os.getppid(),"结束执行")
执行结果:
"C:\Program Files\Python36\python.exe" E:/作业/陈储兵-L009-代码练习1.py 100 8656 5412 开始执行 322 并发中 544 并发中 766 并发中 100 6688 8656 开始执行 100 5264 8656 开始执行 100 5900 8656 开始执行 766 8656 5412 结束执行 100 6688 8656 结束执行 100 5264 8656 结束执行 100 5900 8656 结束执行 i = 0,子进程id >>> 6688,父进程id >>> 8656. i = 1,子进程id >>> 5264,父进程id >>> 8656. i = 2,子进程id >>> 5900,父进程id >>> 8656. Process finished with exit code 0
原文:https://www.cnblogs.com/kaishirenshi/p/11037217.html