我正在尝试使用tkinter创建一个基本控制台窗口。我当前的代码有两个类,一个用于gui,另一个用于向控制台添加文本。文本添加类在不同于gui(在主线程中运行)的线程中运行。我设置了一个队列来在两个线程之间进行通信。除了队列之外,其他一切都正常。当我向队列中添加文本时,它可以从辅助线程读取,但不能从主线程读取。这是我的代码:
import threading, sys, os, time, Queue
from Tkinter import *
class consolegui:
"""Main GUI for the program"""
def __init__(self, root):
self.root=root
self.cout=Text(self.root, width=80)
self.root.after(100, self.process_queue)
self.cout.config(state=DISABLED)
self.cout.pack(side=TOP)
self.bottomf=Frame(self.root, height=1, width=80)
self.bottomf.pack(side=BOTTOM)
self.cin=Entry(self.bottomf, width=100)
self.cin.pack(side=LEFT)
self.enter=Button(self.bottomf, text="send", command=self.on_click)
self.cin.bind('<Return>', self.on_click)
self.enter.pack(side=BOTTOM)
self.queue = Queue.Queue()
worker(self.queue).start()
def on_click(self, *args):
self.cout.config(state=NORMAL)
self.cout.insert(END, self.cin.get()+'n')
self.cout.config(state=DISABLED)
self.cin.delete(0, END)
def add_text(self, text):
self.cout.insert(END, text+'n')
def process_queue(self):
print "reading queue"
try:
msg = self.queue.get(0)
print "found items in queue!"
self.add_text(msg)
with self.queue.mutex:
self.queue.queue.clear()
except Queue.Empty:
print "found nothing"
self.root.after(100, self.process_queue)
class worker(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
time.sleep(5)
print "adding to queue"
self.queue.put("Task finished")
print self.queue.get(0)
if __name__ == "__main__":
root = Tk()
console=consolegui(root)
root.mainloop()
谢谢你的帮助!
您确定它不能从主线程读取吗?您使用的是非阻塞get
,并且在检查之间处于睡眠状态。工作进程只向队列中添加一个项目,并立即将其读回(清空队列)。您创建的竞赛条件要求Python GIL在两次检查之间的精确100秒标记处,在put
和get
之间完全切换到主线程(正如所写的,看起来您在5秒后添加,所以可能还有大约95秒的时间,而这场竞赛永远不会发生)。
简短回答:Queue
中的条目只能get
一次。如果工作线程立即读取,主线程就不能读取(反之亦然)。