多进程中的父进程与子进程 - downtiser/python-one GitHub Wiki
下面这个程序可以用于查看在pychram下运行时该程序时程序本身的pid和父进程pid,同时又在其中起了一个线程,也做同样操作
-
import multiprocessing, os def process_info(): print('the parent pid>>>',os.getppid()) #返回调用此函数的进程的父进程的id print('the pid>>>',os.getpid()) #返回调用此函数的进程id def f(name): print('this is',name) process_info() if __name__ == '__main__': f('the main process') p = multiprocessing.Process(target=f, args=('the children process',)) p.start()
-
this is the main process the parent pid>>> 584 the pid>>> 11500 this is the children process the parent pid>>> 11500 the pid>>> 11572
- 从上面的结果可以看出主程序的pid是11500,其父进程pid是584,这个584进程从何而来,猜想是pychram本身启动了该程序,所以可能是pychram,打开任务管理器查找,可得结果如下
果然是这样,另外,可以看出子进程的父进程pid正好是主程序的pid。从以上结果可以看出,每个进程都是由父进程产生的. 在Linux上,所有进程都是由pid为1的根进程产生 - 之后再cmd上运行该程序,得结果如下
-
this is the main process the parent pid>>> 14436 the pid>>> 5244 this is the children process the parent pid>>> 5244 the pid>>> 3744
- 在任务管理器中再查询14436进程,正是cmd进程
-