I am trying to connect raw_input() to a Tkinter gui, by using Queus and Threading. Here is my code:
import Tkinter
import time
import threading
import Queue
class UI(Tkinter.Frame):
def __init__(self, master=None, queue=None):
self.queue = queue
Tkinter.Frame.__init__(self, master)
self.master.title("what does console say")
self.master.minsize(40, 30)
swd = self.master.winfo_screenwidth()
fwd = 320
fht = 240
self.master.geometry('{0:d}x{1:d}+{2:d}+{3:d}'.format(
fwd, fht, swd - fwd, 0))
self.message = Tkinter.StringVar()
mesgbox = Tkinter.Label(master, textvariable=self.message)
mesgbox.pack(fill=Tkinter.BOTH, expand=1)
self.pack()
self.processqueue()
def processqueue(self):
try:
message = '\n'.join([self.message.get(),
self.queue.get_nowait()])
except Queue.Empty:
pass
else:
self.message.set(message)
self.master.after(100, self.processqueue)
class ThreadedTask(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.daemon = 1
self.queue = queue
def run(self):
i = 0
while True:
i += 1
message = raw_input('put some words:')
self.queue.put("[Message] {0:s}".format(message))
queue = Queue.Queue()
gui = UI(queue=queue)
job = ThreadedTask(queue)
job.start()
gui.mainloop()
However, I get the following error:
error in background error handler:
out of stack space (infinite loop?)
while executing
"::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?)
("after" sc..."
Could anyone help me with it? Thank you in advance!
Another thing is that, this code works if I do not use the raw_input(), but some machine generated text, i.e.:
def run(self):
i = 0
while True:
i += 1
time.sleep(0.5)
self.queue.put("[Message] {0:d}".format(i))
Could anyone explain why?
After a deep searching, I found this post: here
I am not sure if this is exactly the problem, but it inspired me, in the way that I reformed my code, by putting the gui mainloop() in a second thread.
In this way, it works like a charm, although I still don't exactly why there is such behaviour.
Related
The context:
I'm building a Graphical Interface with Qt creator and the "behaviour" file in python. A test version of my GUI is:
The expected behaviour:
I am running 2 different threads which are referred to the same function with different input arguments. With the SELECTOR button I can assign the value of 1 or 2 to a variable (and display it)
The button Start thread enables the correct thread to start (the first time).
The loop should be turned off by the stop button by modifying the global running variable.
This is my code
# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui, uic
import sys
import threading
import time
import Queue
running = False
first_thread = None
second_thread = None
form_class = uic.loadUiType("simple2.ui")[0]
q = Queue.Queue()
select = 0
def action(string, queue): #function called by threads
global running
while(running):
phrase = string
if queue.qsize() < 10:
queue.put(phrase)
#else:
# print queue.qsize()
class MyWindowClass(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
#buttons
self.startButton.clicked.connect(self.start_clicked)
self.stopButton.clicked.connect(self.stop_clicked)
self.selector.clicked.connect(self.sel_click)
#variables
self.first = False
self.second = False
#queue
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.update_phrase)
self.timer.start(1)
def start_clicked(self): #start button callback
global select
if select > 0:
global running
running = True
print "started"
if (not self.first) & (select == 1):
first_thread.start()
self.first = True
if (not self.second) & (select == 2):
second_thread.start()
self.second = True
self.startButton.setEnabled(False)
self.startButton.setText('Starting...')
def stop_clicked(self): #stop button callback
global running
running = False
print "stopped"
self.startButton.setEnabled(True)
self.startButton.setText('Start Thread')
def sel_click(self): #selector button callback
global select
if select < 2:
select = select + 1
else:
select = 1
self.thread_counter.setText(str(select))
def update_phrase(self): #looping function
global running
if (not q.empty()) & running:
self.startButton.setText('Thread on')
abc = q.get()
print abc
def closeEvent(self, event):
global running
running = False
if __name__ == "__main__":
first_thread = threading.Thread(target=action, args = ("first", q))
second_thread = threading.Thread(target=action, args = ("second", q))
app = QtGui.QApplication(sys.argv)
w = MyWindowClass(None)
w.setWindowTitle('Multiple threads test in python')
w.show()
app.exec_()
For now, each thread should simple print on terminal their arguments ("First" or "Second").
If threads are started for the first time, my code works. But I would like to switch between threads infinite times.
Since threads cannot be stopped, is there a way to "pause" them?
I cannot find a solution, I hope someone will help me also with a piece of code. Thank you in advance
You can use Lock class to do that, a simple example would be:
import threading
lock = threading.Lock()
//here it will be lock
lock.acquire() # will block if lock is already held
...
then in other side do
//this will wake up
lock.release()
you can read more here http://effbot.org/zone/thread-synchronization.htm
I want to set a function to repeat again again after the message box is shown.
A short example of code is shown below that what i exactly want to do
def setInterval(func,time,args):
e = threading.Event()
while not e.wait(time):
func(args)
def foo(data):
print data
aa("what")
def aa(a):
print(a)
tkMessageBox.showinfo("Regret","nope")
setInterval(foo,5,"fsrty")
All things are okay but the problem is only that once the messgae box is shown its give "not responding error".Anyone please help to find out the solution
You will need to use the .after method since threading never work well this tkinter and nor does a while loop.
import Tkinter as tk
import TkMessageBox
def setInterval(func,time,args):
func(args)
root.after(time, setInterval(func, time, args))
root.tk.Tk()
root.withdraw()
def foo(data):
print data
aa("what")
def aa(a):
print(a)
tkMessageBox.showinfo("Regret","nope")
setInterval(foo, 5, "fsrty")
root.mainloop()
Threading and Tk don't mix well, as they violate the Tcl/Tk threading model of using Tk just from one thread and your code doesnt work, because you occupied the main (one and only) thread!
While your print-functions keeps printing - GUI is unresponsible because of that and you can check it easily with another print in your code:
>>> print('**** Active threads: %d ****' % threading.active_count())
**** Active threads: 1 ****
So what you really need is just create another one!
try:
import tkinter as tk
from tkinter import messagebox as msgbox
except ImportError:
import Tkinter as tk
import TkMessageBox as msgbox
import threading
def setInterval(func,time,args):
e = threading.Event()
while not e.wait(time):
print('**** Active threads: %d ****' % threading.active_count())
func(args)
def foo(data):
print(data)
aa("what")
def aa(a):
print(a)
root = tk.Tk()
print('**** Active threads: %d ****' % threading.active_count())
thread = threading.Thread(target=setInterval, args=(foo, 5, "fsrty"))
msgbox.showinfo("Regret", "nope")
thread.start()
root.mainloop()
But here's another problem - threads keep running even after your close GUI (when you escaped mainloop)! So if you trying to achieve something simple - .after() is an option (quick and small alternative).
But if you stubborny or really need this - create (override) a custom subclass of the thread object, it's gives you more flexibility to control your flow!
try:
import tkinter as tk
from tkinter import messagebox as msgbox
except ImportError:
import Tkinter as tk
import TkMessageBox as msgbox
import threading
class Thread(threading.Thread):
def __init__(self):
super(Thread, self).__init__()
self.running = False
self.function_to_execute = None
self._stop = threading.Event()
def start_thread(self, func, *args, **kwargs):
self.function_to_execute = (func, args, kwargs)
self.running = True
self.start()
def run(self):
print('### STARTED ###')
while self.running:
try:
print('### RUNNING ###')
function, args, kwargs = self.function_to_execute
function(*args, **kwargs)
except:
self.stop()
def stop(self):
print('### STOPPING ###')
self.running = False
self._stop.set()
def setInterval(func, time, args):
e = threading.Event()
e.wait(time)
print('**** Active threads: %d ****' % threading.active_count())
func(args)
def foo(data):
print(data)
aa('what')
def aa(a):
print(a)
def clear():
thread.stop()
while True:
try:
thread.is_alive()
except TypeError:
root.destroy()
print('### STOPPED ###')
print('**** Active threads: %d ****' % threading.active_count())
break
root = tk.Tk()
thread = Thread()
print('**** Active threads: %d ****' % threading.active_count())
msgbox.showinfo("Regret", "nope")
thread.start_thread(setInterval, foo, 5, 'fsrty')
root.protocol('WM_DELETE_WINDOW', clear)
root.mainloop()
As you see - things are getting more complicated when we trying to clear out before exiting, but it's works!
Conclusion:
For simple things - .after() is a good option!
For complicated and long-executed things - threading is your choise!
Links:
after() method
Tkinter: How to use threads to preventing main event loop from “freezing”
Tkinter and Threads
Also, when things getting complicated: Best way to structure a tkinter application
Please explain how do we send/receive data from Thread managed by Queue....
First I subclass 'QThread' defining its run() method which is started when QThread's.start() is called:
class SimpleThread(QtCore.QThread):
def __init__(self, queue, parent=None):
QtCore.QThread.__init__(self, parent)
self.queue=queue
def run(self):
while True:
arg=self.queue.get()
self.fun(arg)
self.queue.task_done()
def fun(self, arg):
for i in range (3):
print 'fun: %s'%i
self.sleep(1)
return arg+1
Then I declare two Thread instances (so only two CPU cores are taken) sending self.queue instance as an argument.
self.queue=queue.Queue()
for i in range(2):
thread=SimpleThread(self.queue)
thread.start()
Now if I understand it correctly thread.start() is not starting anything. The real "start" happens only when I call queue.put():
for arg in [1,2,3]: self.queue.put(arg)
This last line is what makes a "real" call. Aside from creating and starting Queue item put() allows to save any arbitrary value to each Queue item. .put() does several things at once: it creates, it starts, it moves the processing through the Queue and it allows to place a variable "inside" of the queue item (which later can be retrieved from inside of the function-processor: using Queue item's '.get()` method).
But how do I return the value from fun() function. A "regular" fun()'s return resultValue doesn't work. And I can't use self.queue.put() method since this method aside from storing a data "creates" a new queue item...
EDITED LATER:
Here is slightly tweaked code (copy/pasted from another post) showing an approach on how to return a value from completed Thread. I am not sure if the the approach used here would work with QThread... please correct me if I am wrong:
import os, sys
import threading
import Queue
def callMe(incomingFun, daemon=False):
def execute(_queue, *args, **kwargs):
result=incomingFun(*args, **kwargs)
_queue.put(result)
def wrap(*args, **kwargs):
_queue=Queue.Queue()
_thread=threading.Thread(target=execute, args=(_queue,)+args, kwargs=kwargs)
_thread.daemon=daemon
_thread.start()
_thread.result_queue=_queue
return _thread
return wrap
#callMe
def localFunc(x):
import time
x = x + 5
time.sleep(5)
return x
thread=localFunc(10)
# this blocks, waiting for the result
result = thread.result_queue.get()
print result
In normal circumstances you'd use a result queue to send results back, and then have some other thread running that waits for the results:
class SimpleThread(QtCore.QThread):
def __init__(self, queue, result_queue, parent=None):
QtCore.QThread.__init__(self, parent)
self.queue=queue
self.result_queue = result_queue
def run(self):
while True:
arg=self.queue.get()
self.fun(arg)
self.queue.task_done()
def fun(self, arg):
for i in range (3):
print 'fun: %s'%i
self.sleep(1)
self.result_queue.put(arg+1)
def handle_results(result_queue):
while True:
result = result_queue.get()
print("Got result {}".format(result))
Main thread:
self.queue=queue.Queue()
self.result_queue = queue.Queue()
result_handler = threading.Thread(target=handle_results, self.result_queue)
for i in range(2):
thread=SimpleThread(self.queue, self.result_queue)
thread.start()
Doing it this way will keep you from blocking the GUI's event loop while you wait for the results. Here's what the equivalent would look like with multiprocessing.pool.ThreadPool:
from multiprocessing.pool import ThreadPool
import time
def fun(arg):
for i in range (3):
print 'fun: %s'%i
time.sleep(1)
return arg+1
def handle_result(result):
print("got result {}".format(result))
pool = ThreadPool(2)
pool.map_async(fun, [1,2,3], callback=handle_result)
Which is a lot simpler. It internally creates a result handling thread, which will automatically call handle_result for you when fun completes.
That said, you're using QThread, and you want the results to update GUI widgets, so you really want your results to be sent back to the main thread, not to a result handling thread. In that case, it makes sense to use Qt's signaling system, so that you can safely update the GUI when you receive the result:
from PyQt4 import QtCore, QtGui
import sys
import Queue as queue
class ResultObj(QtCore.QObject):
def __init__(self, val):
self.val = val
class SimpleThread(QtCore.QThread):
finished = QtCore.pyqtSignal(object)
def __init__(self, queue, callback, parent=None):
QtCore.QThread.__init__(self, parent)
self.queue = queue
self.finished.connect(callback)
def run(self):
while True:
arg = self.queue.get()
if arg is None: # None means exit
print("Shutting down")
return
self.fun(arg)
def fun(self, arg):
for i in range(3):
print 'fun: %s' % i
self.sleep(1)
self.finished.emit(ResultObj(arg+1))
class AppWindow(QtGui.QMainWindow):
def __init__(self):
super(AppWindow, self).__init__()
mainWidget = QtGui.QWidget()
self.setCentralWidget(mainWidget)
mainLayout = QtGui.QVBoxLayout()
mainWidget.setLayout(mainLayout)
button = QtGui.QPushButton('Process')
button.clicked.connect(self.process)
mainLayout.addWidget(button)
def handle_result(self, result):
val = result.val
print("got val {}".format(val))
# You can update the UI from here.
def process(self):
MAX_CORES=2
self.queue = queue.Queue()
self.threads = []
for i in range(MAX_CORES):
thread = SimpleThread(self.queue, self.handle_result)
self.threads.append(thread)
thread.start()
for arg in [1,2,3]:
self.queue.put(arg)
for _ in range(MAX_CORES): # Tell the workers to shut down
self.queue.put(None)
app = QtGui.QApplication([])
window = AppWindow()
window.show()
sys.exit(app.exec_())
Output when the button is pushed:
fun: 0
fun: 0
fun: 1
fun: 1
fun: 2
fun: 2
fun: 0
got val 2
got val 3
Shutting down
fun: 1
fun: 2
Shutting down
got val 4
This is one part of a two part question (other part is here)
So here's what I'm looking for: A function which is bound to the EVT_TEXT event of a text control that waits a few seconds, then calls another function at the end of the delay time. Thats easy enough, but, I'd like it to reset the delay time every time a new EVT_TEXT event is generated. The effect I'm looking for is to have a user type into the text control, then after I assume they're done, I run the function described in the other part of this question which spell checks what they've written.
So the simple approach I tried was this:
def OnEdit(self, event):
for i in range(0,3):
print i
time.sleep(1)
However, this just forces a 3 second wait, no matter what. How do I "break in" to this function to reset the counter? Thanks in advance.
EDIT: Turns out the way to do this was with threading. Yippee
The full threading answer, built with the help of this tutorial:
from threading import *
import wx
import time
EVT_RESULT_ID = wx.NewId()
def EVT_RESULT(win, func):
win.Connect(-1, -1, EVT_RESULT_ID, func)
class MyGui(wx.Frame):
def __init__(self):
self.spellchkthrd = None
#lots of stuff
self.input = wx.TextCtrl(self.panel, -1, "", size=(200, 150), style=wx.TE_MULTILINE|wx.TE_LEFT|wx.TE_RICH)
self.Bind(wx.EVT_TEXT, self.OnEdit, self.input)
EVT_RESULT(self, self.OnSplCheck)
def OnEdit(self, event):
if not self.spellchkthrd:
self.spellchkthrd = SpellCheckThread(self)
else:
self.spellchkthrd.newSig()
def OnSplCheck(self, event):
self.spellchkthrd = None
#All the spell checking stuff
class ResultEvent(wx.PyEvent):
def __init__(self):
wx.PyEvent.__init__(self)
self.SetEventType(EVT_RESULT_ID)
class SpellCheckThread(Thread):
def __init__(self, panel):
Thread.__init__(self)
self.count = 0
self.panel = panel
self.start()
def run(self):
while self.count < 1.0:
print self.count
time.sleep(0.1)
self.count += 0.1
wx.PostEvent(self.panel, ResultEvent())
def newSig(self):
print "new"
self.count = 0
I have a simple chat client that I was attempting to get working with Tkinter as the interface. My problem is that when running the mainloop with .after for the chat input/output, the window freezes and blocks until another message is received.
class Client(Frame):
def __init__(self, **kwargs):
Frame.__init__(self, Tk())
self.pack()
self.lb = Listbox(self, width=100, height=30)
self.lb.pack()
self.show_data = self.lb.after(1000, self.chat_handle)
self.entry = Entry(self)
self.entry.bind('<Return>', self.input_handle)
self.entry.pack(side=BOTTOM, fill=X)
def input_handle(self, event):
msg = self.entry.get()
self.entry.delete(0, 'end')
new_msg = 'privmsg %s :' % self.channel + msg + '\r\n'
self.client.sendall(new_msg)
self.lb.insert(END, self.nick + ' | ' + msg)
def chat_handle(self):
try:
self.data = self.client.recvfrom(1024)
except socket.error:
self.lb.insert(END, "Bad Connection!")
return
if self.data and len(self.data[0]) > 0:
self.lb.insert(END, self.data[0])
elif self.data and len(self.data[0]) == 0:
self.lb.insert(END, "Connection Dropped!")
return
self.show_data = self.lb.after(1000, self.chat_handle)
This block of code is shortened but, shows the relavent parts involved. The Entry widget will become unresponsive for extended periods while .after is called and won't respond until a message is received.
When the Entry widget becomes responsive again, the entry field has all the data that was typed in but, I won't see the changes during the "frozen" time. The same goes for the Listbox widget.
If anyone could shed some light on why this is exactly or point out if I'm miss using a method here, it would be greatly appreciated.
EDIT: after some more research, its looking like the socket data is blocking whenever its called and window is getting frozen during this time.
after executes the callback function after the given time; however, this method also runs in the main thread. So if there is an operation that takes more time than usual (in this case, the blocking recvfrom), the GUI will be unresponsive until the complete callback is executed.
To solve this, a common recipe is to spawn a new thread and communicate it with your Tkinter code with a synchronized object like a Queue. Thus, you put the data in the queue when you receive it from the socket, and then check periodically in the main thread inside the after callback.
This is a question whose answer can be adapted to use the same approach: Tkinter: How to use threads to preventing main event loop from "freezing"
I learned about using select to make system calls to check if a socket file is ready to be read.
How to set timeout on python's socket recv method?
class Client(Frame):
def __init__(self, **kwargs):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client.connect(("host", port))
self.client.setblocking(0)
Frame.__init__(self, Tk())
self.pack()
self.lb = Listbox(self, width=100, height=30)
self.lb.pack()
self.show_data = self.lb.after(1000, self.chat_handle)
self.entry = Entry(self)
self.entry.bind('<Return>', self.input_handle)
self.entry.pack(side=BOTTOM, fill=X)
def input_handle(self, event):
msg = self.entry.get()
self.entry.delete(0, 'end')
new_msg = 'privmsg %s :' % self.channel + msg + '\r\n'
self.client.sendall(new_msg)
self.lb.insert(END, self.nick + ' | ' + msg)
def chat_handle(self):
socket_data = select.select([self.client], [], [], 0.3) # set timeout on last arg
if socket_data[0]:
try:
self.data = self.client.recvfrom(1024)
except socket.error:
self.lb.insert(END, "Bad Connection!")
return
if self.data and len(self.data[0]) > 0:
self.lb.insert(END, self.data[0])
elif self.data and len(self.data[0]) == 0:
self.lb.insert(END, "Connection Dropped!")
return
self.show_data = self.lb.after(1000, self.chat_hand