[关闭]
@chenwei123 2018-02-06T02:27:14.000000Z 字数 666 阅读 374

进程&线程

Python


  1. from multiprocessing import Process
  2. import os
  3. def run_proc(name):
  4. print("Run child process %s (%s)..." %(name, os.getpid()))
  5. if __name__ == '__main__':
  6. print("Parent process %s." % os.getpid())
  7. p=Process(targent=run_proc, args=('test',))
  8. print('Child process will start.')
  9. p.start() #启动
  10. p.join() #等待子进程结束后再继续往下运行
  11. print("Child process end.")
  1. from multiprocessing import Pool
  2. import os, time, random
  3. def long_time_task(name):
  4. print("Run task %s (%s)..." % name, os.getpid()))
  5. start=time.time()
  6. time.sleep(random.random()*3)
  7. end=time.time()
  8. print("Task %s runs %0.2f seconds." % (name, (end-start)))
  9. if __name__ == "__main__":
  10. print("Parent process %s." % os.getpid())
  11. p = Pool(4)
  12. for i in range(5):
  13. p.apply_async(long_time_task, args=(i,))
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注