37,719
社区成员
发帖
与我相关
我的任务
分享
import threading
import sys
from Queue import Queue
class Demo:
def __init__(self):
self.queue=Queue()
self.end='__end__'
def read(self):
while 1:
line=sys.stdin.readline()
if not line:
self.queue.put(self.end)
break
if len(line.strip())==0:
continue
self.queue.put(line)
print '--- read end ---'
def write(self):
i=1
while 1:
if not self.queue.empty():
line=self.queue.get_nowait()
if line==self.end:
break
print "%d--%s" % (i,line)
i+=1
print '--- write end ---'
def start(self):
threads=[]
t=threading.Thread(target=self.read)
threads.append(t)
t=threading.Thread(target=self.write)
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
print '--- all end ---'
demo=Demo()
demo.start()