求助!!!关于Python主线程如何挂起!!!
小弟写了个小程序,主线程会启动两个子线程,两个子线程一个不停的从标准输入读数据,另一个不停的处理数据,写入文件,两个都是While循环不停止的。
我现在是用tail -f 命令从管道导出数据到我的.py,但是每次运行的时候都需要我Ctrl+C一下才能运行新线程。我估计是主线程没有挂起所以不会唤醒两个子线程,急求该如何挂起主线程让两个线程运行!!!!!!或者哪位大牛帮我分析一下是不是我设计出错了。。。 python 线程
[解决办法]
根据你的意思,我仿写了一下,给你参考一下。
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()