首页 > 代码库 > 0704 Process继承实现多进程、Pool进程池,及Pool实现多进程实现复制文件

0704 Process继承实现多进程、Pool进程池,及Pool实现多进程实现复制文件

 

 

 

通过继承的方式,实现Process多进程

 1 from multiprocessing import Process
 2 import time
 3 
 4 class MyNewProcess(Process):
 5     def run(self):
 6         for i in range(10):
 7             print("----run----")
 8             time.sleep(1)
 9 
10 
11 if __name__ == "__main__":
12     p = MyNewProcess()
13 
14     p.start()        # Process 中的 start 方法会主动调用 run 方法
15 for i in range(10): print("---main---") time.sleep(1)

 

Pool 进程池实现多进程

 1 import time
 2 from multiprocessing import Pool
 3 
 4 def worker():
 5     for i in range(10):
 6         print("From worker %s"%i)
 7         time.sleep(0.5)
 8 
 9 def foo():
10     for i in range(10):
11         print("From foo %s"%i)
12         time.sleep(0.5)
13 
14 def bar():
15     for i in range(10):
16         print("From bar %s"%i)
17         time.sleep(0.5)
18 
19 if __name__ == "__main__":
20     pool = Pool(3)            # 创建三个 进程
21     pool.apply_async(worker)
22     pool.apply_async(foo)
23     pool.apply_async(bar)
24 
25     pool.close()                  # 关闭进程池,禁止添加任务
26     pool.join()          # 等待子进程结束后,主进程才往下走
27     print("Is done...")
28 
29 
30 
31 # process and Pool 最后都是调用 fork
32 # 通常情况下,主进程一半用来等低啊,,,,真正的任务子进程中执行

 

利用Pool进程池实现简单的文件复制

 1 import os
 2 import time
 3 from multiprocessing import Pool
 4 
 5 def copyFile(oldPath, newPath, fileName):
 6     print("%s 准备复制中。。。"%fileName)
 7     with open("%s\%s"%(oldPath, fileName), r) as fr, open("%s\%s"%(newPath, fileName), w) as fw:
 8         for line in fr:
 9             fw.write(line)
10     time.sleep(1)
11 
12 
13 if __name__ == "__main__":
14     oldPath = r"file"
15     newPath = r"file-副本"
16 
17     os.mkdir(newPath)
18 
19     pool = Pool(5)
20 
21     fileList = os.listdir(oldPath)
22 
23     for fileName in fileList:
24         pool.apply_async(copyFile, (oldPath, newPath, fileName))
25 
26     pool.close()
27 
28     pool.join()
29 
30     print("文件复制完成....")

 

0704 Process继承实现多进程、Pool进程池,及Pool实现多进程实现复制文件