1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import os 5 import time 6 # fork只能用于linux/unix中 7 pid = os.fork() 8 print('zy') 9 if pid == 0: 10 # os.getpid()当前进程的id,os.getppid()当前进程的父进程的id 11 # 子进程会把父进程所有的数据拷贝一份,fork()之后的代码会重新运行一遍 12 print('子进程 {} , 父进程是:{} .'.format(os.getpid(), os.getppid())) 13 else: 14 print('我是父进程:{}'.format(pid)) 15 16 # 父进程运行完成就退出,子进程不会退出 17 time.sleep(2)
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import multiprocessing 5 import time 6 7 8 # 多进程编程 9 def get_html(n): 10 time.sleep(n) 11 print('sub progress success') 12 return n 13 14 15 if __name__ == '__main__': 16 progress = multiprocessing.Process(target=get_html, args=(2,)) 17 print(progress.pid) 18 progress.start() 19 print(progress.pid) 20 progress.join() 21 print('main progress end') 22 23 # 使用进程池 24 pool = multiprocessing.Pool(multiprocessing.cpu_count()) 25 # 提交任务 26 result = pool.apply_async(get_html, args=(3,)) 27 28 # 关闭pool,不再接收新的任务 29 pool.close() 30 # 等待所有任务完成 31 pool.join() 32 print(result.get())
None 12052 sub progress success main progress end sub progress success 3
imap
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import multiprocessing 5 import time 6 7 8 # 多进程编程 9 def get_html(n): 10 time.sleep(n) 11 print('sub progress success') 12 return n 13 14 15 if __name__ == '__main__': 16 # 使用进程池 17 pool = multiprocessing.Pool(multiprocessing.cpu_count()) 18 19 # imap,按照顺序打印 20 for result in pool.imap(get_html, [1, 5, 3]): 21 print('{} sleep suceess'.format(result))
sub progress success 1 sleep suceess sub progress success sub progress success 5 sleep suceess 3 sleep suceess
imap_unordered
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 import multiprocessing 5 import time 6 7 8 # 多进程编程 9 def get_html(n): 10 time.sleep(n) 11 print('sub progress success') 12 return n 13 14 15 if __name__ == '__main__': 16 # 使用进程池 17 pool = multiprocessing.Pool(multiprocessing.cpu_count()) 18 19 # imap,哪个先执行完打印哪个 20 for result in pool.imap_unordered(get_html, [1, 5, 3]): 21 print('{} sleep suceess'.format(result))
sub progress success 1 sleep suceess sub progress success 3 sleep suceess sub progress success 5 sleep suceess