本文内容参考 https://www.liaoxuefeng.com/wiki/1016959663602400/1017628290184064
多进程
os模块的fork()
多进程(multiprocessing):Unix/Linux操作系统提供了一个fork()函数,fork()函数调用时,操作系统自动把当前进程(父进程)复制了一份(子进程),然后分别在父进程和子进程内返回。 子进程永远返回0,而父进程返回子进程的ID。import os
print("Parent Process ({}) start".format(os.getpid()))
# 下面的5行代码只在Unix/Linux/Mac系统生效,windows系统os没有fork
try:
pid = os.fork()
if pid == 0:
print("child process ({}) ,parent process ({})".format(os.getpid(), os.getppid()))
else:
print("parent process ({}) fork a child process ({}).".format(os.getpid(), pid))
except AttributeError as err:
print(err)
windows环境输出
Parent Process (4612) start
module 'os' has no attribute 'fork'
multiprocessing模块
windows 环境中多进程可以使用multiprocessing模块 创建子进程时,只需要传入一个执行函数和函数的参数,创建一个Process实例,用start()方法启动,join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步。from multiprocessing import Process
# 子进程要执行的代码
def run_proc(name):
print("Run child process {} ({})...".format(name, os.getpid()))
time.sleep(random.random())
print("child process {} ({}) done".format(name, os.getpid()))
# 创建子进程时,只需要传入一个执行函数和函数的参数,创建一个Process实例,用start()方法启动,join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步。
if __name__ == "__main__":
print("Parent Process ({}) start...".format(os.getpid()))
p = Process(target=run_proc, args=("test",))
print("Child process will start.")
p.start()
p.join()
print("Child process end.")
输出
Parent Process (6304) start...
Child process will start.
Run child process test (20676)...
child process test (20676) done
Child process end.
测试过程中发现了一个问题:
from multiprocessing import Process
print("Parent Process ({}) start".format(os.getpid()))
def run_proc(name):
print("Run child process {} ({})...".format(name, os.getpid()))
time.sleep(random.random())
print("child process {} ({}) done".format(name, os.getpid()))
if __name__ == "__main__":
p = Process(target=run_proc, args=("test",))
print("Child process will start.")
p.start()
p.join()
print("Child process end.")
输出内容让我觉得有些迷糊:
Parent Process (22880) start
Child process will start.
Parent Process (16272) start
Run child process test (16272)...
child process test (16272) done
Child process end.
还有
from multiprocessing import Process
print("Parent Process ({}) start".format(os.getpid()))
def run_proc(name):
print("Run child process {} ({})...".format(name, os.getpid()))
time.sleep(random.random())
print("child process {} ({}) done".format(name, os.getpid()))
if __name__ == "__main__":
print("Parent Process ({}) start...".format(os.getpid()))
p = Process(target=run_proc, args=("test",))
print("Child process will start.")
p.start()
p.join()
print("Child process end.")
输出
Parent Process (18116) start
Parent Process (18116) start...
Child process will start.
Parent Process (11684) start
Run child process test (11684)...
child process test (11684) done
Child process end.
进程池
import time, random
from multiprocessing import Process, Pool
def run_proc(name):
print("Run child process {} ({})...".format(name, os.getpid()))
time.sleep(random.random())
print("child process {} ({}) done".format(name, os.getpid()))
# 进程池
if __name__ == "__main__":
print("Parent Process ({}) start...".format(os.getpid()))
p = Pool(4)
for i in range(5):
p.apply_async(run_proc, args=(i,))
print("Waiting for all subprocesses done...")
p.close()
p.join()
print("All subprocesses done.")
输出
Parent Process (836) start...
Waiting for all subprocesses done...
Run child process 0 (19948)...
Run child process 1 (5084)...
Run child process 2 (14552)...
Run child process 3 (7928)...
child process 0 (19948) done
child process 2 (14552) done
Run child process 4 (19948)...
child process 1 (5084) done
child process 3 (7928) done
child process 4 (19948) done
All subprocesses done.
标签:Process,process,start,线程,child,print,之多,os,python3 From: https://www.cnblogs.com/caroline2016/p/17969569