forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiprocess.py
More file actions
44 lines (37 loc) · 998 Bytes
/
multiprocess.py
File metadata and controls
44 lines (37 loc) · 998 Bytes
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
36
37
38
39
40
41
42
43
44
# encoding: utf-8
__author__ = 'zhanghe'
from multiprocessing import Process, Queue
import time
import random
def write(q):
"""
写数据进程执行的代码
:param q:
:return:
"""
for value in ['A', 'B', 'C']:
print 'Put %s to queue...' % value
q.put(value)
time.sleep(random.random())
def read(q):
"""
读数据进程执行的代码
:param q:
:return:
"""
while True:
value = q.get(True)
print 'Get %s from queue.' % value
if __name__ == '__main__':
# 父进程创建Queue,并传给各个子进程,一个写,一个读,从而实现进程间通信:
queue = Queue()
pw = Process(target=write, args=(queue,))
pr = Process(target=read, args=(queue,))
# 启动子进程pw,写入:
pw.start()
# 启动子进程pr,读取:
pr.start()
# 等待pw结束:
pw.join()
# pr进程里是死循环,无法等待其结束,只能强行终止:
pr.terminate()