python 进程
linux 多进程
Unix/Linux操作系统提供了一个fork()系统调用
特点:
- fork()调用一次,返回两次
- 当前进程复制了一份(父进程),生成子进程
- - 子进程返回0
- 父进程返回子进程的ID
fork 只存在linux,windows无法运行
linux 运行:
import os
print('Process (%s) start...' % os.getpid())
# Only works on Unix/Linux/Mac:
pid = os.fork()
if pid == 0:
print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
else:
print('I (%s) just created a child process (%s).' % (os.getpid(), pid))
运行结果:
Process (876) start...
I (876) just created a child process (877).
I am child process (877) and my parent is 876.
跨平台多进程
multiprocessing
multiprocessing模块是跨平台版本的多进程模块
例子:
from multiprocessing import Process
import os
# 子进程要执行的代码
def run_proc(name):
print('Run child process %s (%s)...' % (name, os.getpid()))
if __name__=='__main__':
print('Parent process %s.' % os.getpid())
p = Process(target=run_proc, args=('test',))
print('Child process will start.')
p.start()
p.join()
print('Child process end.')
pool
变量共享,使用manager
from multiprocessing import Pool, Manager
def func(my_list, my_dict):
my_list.append(10)
my_list.append(11)
my_dict['a'] = 1
my_dict['b'] = 2
if __name__ == '__main__':
manager = Manager()
my_list = manager.list()
my_dict = manager.dict()
pool = Pool(processes=2)
for i in range(0, 2):
pool.apply_async(func, (my_list, my_dict))
pool.close()
pool.join()
print(my_list)
print(my_dict)