求助!!!关于Python主线程如何挂起!!!

豆沙包大人 2013-08-30 11:25:28
小弟写了个小程序,主线程会启动两个子线程,两个子线程一个不停的从标准输入读数据,另一个不停的处理数据,写入文件,两个都是While循环不停止的。
我现在是用tail -f 命令从管道导出数据到我的.py,但是每次运行的时候都需要我Ctrl+C一下才能运行新线程。我估计是主线程没有挂起所以不会唤醒两个子线程,急求该如何挂起主线程让两个线程运行!!!!!!或者哪位大牛帮我分析一下是不是我设计出错了。。。
...全文
380 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yueguanghaidao 2013-09-04
  • 打赏
  • 举报
回复
根据你的意思,我仿写了一下,给你参考一下。
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()

效果:
SoulRed 2013-08-31
  • 打赏
  • 举报
回复
你的开发环境??windows linux ??
maniachhz 2013-08-31
  • 打赏
  • 举报
回复
贴出代码吧,也让大家了解下
jeky_zhang2013 2013-08-30
  • 打赏
  • 举报
回复
还没写过线程,想学习下,帮顶~~
梅小西Echo 2013-08-30
  • 打赏
  • 举报
回复
代码贴上来啊

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧