Discuss / Python / 把程序改慢一点就能看出是进程并行执行了。

把程序改慢一点就能看出是进程并行执行了。

Topic source

漫步立冬

#1 Created at ... [Delete] [Delete and Lock User]

把程序改慢一点就能看出是进程并行执行了。否则总显得是串行执行。

  5 from multiprocessing import Process
  6 import os, time
  7 
  8 def run_proc(name):
  9     for i in range(10):
 10         time.sleep(0.1)
 11         print(f'Run child process {name}: {os.getpid()}-', i)
 12 
 13 if __name__ == '__main__':
 14     print(f'Parent process: {os.getpid()}')
 15     p = Process(target=run_proc, args=('test',))
 16     print('Child process will start...')
 17     p.start()       
 18     for i in range(10):
 19         time.sleep(0.1)
 20         print(f'Parent process:{os.getpid()}-', i)
 21     p.join()        
 22     print('Child process end.')


  • 1

Reply