multiprocess2.py 655 B

1234567891011121314151617181920212223242526272829303132
  1. """
  2. 实现进程间的通信
  3. Version: 0.1
  4. Author: 骆昊
  5. Date: 2018-03-20
  6. """
  7. import multiprocessing
  8. import os
  9. def sub_task(queue):
  10. print('子进程进程号:', os.getpid())
  11. counter = 0
  12. while counter < 1000:
  13. queue.put('Pong')
  14. counter += 1
  15. if __name__ == '__main__':
  16. print('当前进程号:', os.getpid())
  17. queue = multiprocessing.Queue()
  18. p = multiprocessing.Process(target=sub_task, args=(queue,))
  19. p.start()
  20. counter = 0
  21. while counter < 1000:
  22. queue.put('Ping')
  23. counter += 1
  24. p.join()
  25. print('子任务已经完成.')
  26. for _ in range(2000):
  27. print(queue.get(), end='')