multiprocess2.py 592 字节
Newer Older
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
"""

实现进程间的通信

Version: 0.1
Author: 骆昊
Date: 2018-03-20

"""

import multiprocessing
import os


def sub_task(queue):
	print('子进程进程号:', os.getpid())
	counter = 0
	while counter < 1000:
		queue.put('Pong')
		counter += 1


if __name__ == '__main__':
	print('当前进程号:', os.getpid())
	queue = multiprocessing.Queue()
	p = multiprocessing.Process(target=sub_task, args=(queue,))
	p.start()
	counter = 0
	while counter < 1000:
		queue.put('Ping')
		counter += 1
	p.join()
	print('子任务已经完成.')
	for _ in range(2000):
		print(queue.get(), end='')