Queue本身是一个消息列队程序,可以通过这个模块来进行进程之间的相互通信
[Python]
syntaxhighlighter_viewsource
syntaxhighlighter_copycode
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
from
multiprocessing
import
Process
from
multiprocessing
import
Queue
import
time
def
write(q):
while
True
:
q.put(
"www.c0ks.com"
)
time.sleep(
1
)
def
read(q):
while
True
:
str
=
q.get()
if
(
str
!
=
""):
print
(
str
)
if
__name__
=
=
"__main__"
:
q
=
Queue()
pw
=
Process(target
=
write,args
=
(q,))
pr
=
Process(target
=
read,args
=
(q,))
pw.start()
pr.start()
time.sleep(
10
)
# 强制结束子进程
pw.terminate()
pr.terminate()
|
原文地址:http://www.c0ks.com/thread-5427-1-1.html