进程之间不共享资源的原因

进程之间不共享资源 - 图1

  1. import multiprocessing
  2. import time
  3. g_list = list()
  4. def add_list():
  5. for i in range(1, 5):
  6. g_list.append(i)
  7. time.sleep(0.2)
  8. print(i)
  9. pass
  10. pass
  11. def read_list():
  12. print(g_list)
  13. if __name__ == '__main__':
  14. add_process = multiprocessing.Process(target=add_list)
  15. read_process = multiprocessing.Process(target=read_list)
  16. add_process.start()
  17. # join 是当前进程下(主进程),只有当子进程执行完成之后,才得以往下执行
  18. add_process.join()
  19. read_process.start()
  20. print(g_list)