Подтвердить что ты не робот

Tkinter: Подождите, пока элемент в очереди

Я использую очередь для обмена сообщениями между фоновым потоком и Tk GUI-приложением. В настоящее время это делается путем вызова метода запроса время от времени.

def read_queue(self):
    try:
        self.process(self.queue.get(False)) # non-blocking
    except Queue.Empty:
        pass
    finally:
        self.after(UPDATE_TIME, self.read_queue)

Проблема с этим подходом заключается в том, что если UPDATE_TIME слишком велико, приложение будет обрабатывать новые элементы медленнее, чем это возможно. Если он слишком мал, Tk проводит большую часть времени, проверяя очередь, хотя в то же время он мог делать другие вещи.

Есть ли способ автоматически запускать метод read_queue всякий раз, когда новый элемент поступает в очередь? (Я мог бы называть метод на Tk, когда фоновый поток заполняет очередь, но я боюсь, что это дает мне некоторые проблемы concurrency - вот почему Im использует очереди в конце концов.)

4b9b3361

Ответ 1

РЕЗЮМЕ: я бы не использовал "noob oddy example code" - это принципиально ошибочный подход.

Я не гуру python, но код примера представлен noob oddy "(который вызывает root.event_generate (...) в фоновом потоке ) представляется" фундаментально ошибочным подходом ". то есть в Интернете есть несколько статей, в которых говорится" никогда не ссылаться на функции/методы объекта Tkinter вне контекста "потока GUI" (который обычно является основным потоком). Его пример работает "большую часть времени", но если вы увеличиваете скорость генерации событий, то пример "скорости сбоя" будет увеличиваться, однако конкретное поведение зависит от скорости генерации события и характеристик производительности платформы.

Например, используя свой код с Python 2.7.3, если вы измените:

       time.sleep(1)

в

       time.sleep(0.01)

то приложение script/обычно будет терпеть крах после числа итераций 'x'.

После долгих поисков, если вы "должны использовать Tkinter", то, по-видимому, наиболее "пуленепробиваемый метод" получения информации из фонового потока в поток GUI должен использовать метод "after()" для опроса потока -безопасный объект (например, "Очередь" ). например.

################################################################################
import threading
import time
import Queue
import Tkinter      as Tk
import Tkconstants  as TkConst
from ScrolledText import ScrolledText
from tkFont       import Font

global top
global dataQ
global scrText

def thread_proc():
    x = -1
    dataQ.put(x)
    x = 0
    for i in xrange(5):
        for j in xrange(20):
            dataQ.put(x)
            time.sleep(0.1)
            x += 1
        time.sleep(0.5)
    dataQ.put(x)

def on_after_elapsed():
    while True:
        try:
            v = dataQ.get(timeout=0.1)
        except:
            break
        scrText.insert(TkConst.END, "value=%d\n" % v)
        scrText.see(TkConst.END)
        scrText.update()
    top.after(100, on_after_elapsed)

top     = Tk.Tk()
dataQ   = Queue.Queue(maxsize=0)
f       = Font(family='Courier New', size=12)
scrText = ScrolledText(master=top, height=20, width=120, font=f)
scrText.pack(fill=TkConst.BOTH, side=TkConst.LEFT, padx=15, pady=15, expand=True)
th = threading.Thread(target=thread_proc)
th.start()
top.after(100, on_after_elapsed)
top.mainloop()
th.join()
## end of file #################################################################

Ответ 2

Одним из вариантов может быть mtTkinter http://tkinter.unpythonic.net/wiki/mtTkinter

Вот еще один пример использования event_generate из фонового потока:

##The only secure way I found to make Tkinter mix with threads is to never  
##issue commands altering the graphical state of the application in another  
##thread than the one where the mainloop was started. Not doing that often  
##leads to random behaviour such as the one you have here. Fortunately, one  
##of the commands that seems to work in secondary threads is event_generate,  
##giving you a means to communicate between threads. If you have to pass  
##information from one thread to another, you can use a Queue.
##
##This obviously complicates things a bit, but it may work far better.  
##Please note that the 'when' option *must* be specified in the call to  
##event_generate and *must not* be 'now'. If it not specified or if it  
##'now', Tkinter may directly execute the binding in the secondary thread  
##context. (Eric Brunel)

import threading
import time
import Queue
from Tkinter import *

## Create main window
root = Tk()

## Communication queue
commQueue = Queue.Queue()

## Function run in thread
def timeThread():
    curTime = 0
    while 1:
        ## Each time the time increases, put the new value in the queue...
        commQueue.put(curTime)
        ## ... and generate a custom event on the main window
        try:
            root.event_generate('<<TimeChanged>>', when='tail')
        ## If it failed, the window has been destoyed: over
        except TclError:
            break
        ## Next
        time.sleep(1)
        curTime += 1

## In the main thread, do usual stuff
timeVar = IntVar()
Label(root, textvariable=timeVar, width=8).pack()

## Use a binding on the custom event to get the new time value
## and change the variable to update the display
def timeChanged(event):
    timeVar.set(commQueue.get())

root.bind('<<TimeChanged>>', timeChanged)

## Run the thread and the GUI main loop
th=threading.Thread(target=timeThread)
th.start()

root.mainloop()

Также упоминается использование after_idle аналогичным образом.
т.е.. root.after_idle (timeChanged)

Ответ 3

Опрос можно исключить из решения Ken Mumme, используя os.pipe для синхронизации между двумя потоками.

tkinter имеет метод createFilehandler, который можно использовать для добавления дескриптора файла в цикл выбора tk. Затем вы можете сообщить, что что-то готово в очереди, написав байт в трубе.

Решение выглядит так:

import Queue
import os

uiThreadQueue = Queue.Queue() ;

pipe_read, pipe_write = os.pipe() ;

# call one function from the queue.  Triggered by the 
# pipe becoming readable through root.tk.createfilehandler().
def serviceQueue(file, mask):
    os.read(pipe_read, 1) 
    func = uiThreadQueue.get() ;
    func() 

# enqueue a function to be run in the tkinter UI thread.
# best used as inUIThread(lambda: self.callSomeFunction(arg1,arg2,arg3))
def inUIThread(f):
    uiThreadQueue.put(f)
    os.write(pipe_write, "x")

... set up your widgets, start your threads, etc.....


root.tk.createfilehandler(pipe_read, tkinter.READABLE, serviceQueue)
root.mainloop()

Я не эксперт по python; извините, если я испортил любые соглашения о кодировании. Я отлично разбираюсь в трубах:)