472,779 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

Use threads or Tkinter event loop?

I'm trying to decide whether I need threads in my Tkinter application or
not. My app is a front end to a command-line tool; it feeds commands to
the command-line program, then reads its output and displays it in a
Tkinter text widget. Some of the commands are long-running and/or return
thousands of lines of output.

I initially thought I needed to use threading, because the GUI would
block when reading the output, even when I configured the blocking to be
non-blocking. I got threading to work, but it seemed a bit complicated.
So, I decided to try something simpler, by using the Tkinter event loop
to force the output to update/display.

it seems to work well enough. Here is my threaded code:

non-threaded:

def insertDump(self):
self.finkinstalled = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstalled:
self.t.insert(END, line)
self.update()
self.t.see(END)

And here is my non-threaded code (needs two functions to work)

def insertDump(self):
try:
data = self.dataQueue.get(block=False)
for line in data:
self.t.insert(END, line)
self.t.see(END)
self.update()
except:
print "error"
raise

def getDump(self):

self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue.put(self.file)

This brings me to a design, as opposed to coding, question. The
non-threaded version seems to work just as well as the threaded one, in
terms of speed. Moreover, it is simpler to code and debug, because I
don't have to check to make sure the thread queue has data (I sometimes
get an 'Empty' error message when I first start the thread). Simply
using the Tk event loop (self.update) is also how I would have coded
this in Tcl.

So my question is this: under what circumstances in Python are threads
considered "best practice"? Am I wrong to use the Tk event loop instead
of threads?

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Mar 27 '07 #1
2 9454
Kevin Walzer wrote:
I'm trying to decide whether I need threads in my Tkinter application or
not. My app is a front end to a command-line tool; it feeds commands to
the command-line program, then reads its output and displays it in a
Tkinter text widget. Some of the commands are long-running and/or return
thousands of lines of output.

I initially thought I needed to use threading, because the GUI would
block when reading the output, even when I configured the blocking to be
non-blocking. I got threading to work, but it seemed a bit complicated.
So, I decided to try something simpler, by using the Tkinter event loop
to force the output to update/display.

it seems to work well enough. Here is my threaded code:

non-threaded:

def insertDump(self):
self.finkinstalled = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstalled:
self.t.insert(END, line)
self.update()
self.t.see(END)

And here is my non-threaded code (needs two functions to work)

def insertDump(self):
try:
data = self.dataQueue.get(block=False)
for line in data:
self.t.insert(END, line)
self.t.see(END)
self.update()
except:
print "error"
raise

def getDump(self):

self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue.put(self.file)

This brings me to a design, as opposed to coding, question. The
non-threaded version seems to work just as well as the threaded one, in
terms of speed. Moreover, it is simpler to code and debug, because I
don't have to check to make sure the thread queue has data (I sometimes
get an 'Empty' error message when I first start the thread). Simply
using the Tk event loop (self.update) is also how I would have coded
this in Tcl.

So my question is this: under what circumstances in Python are threads
considered "best practice"? Am I wrong to use the Tk event loop instead
of threads?
D'oh, I got the code snippets mixed up:

non-threaded:

def insertDump(self):
self.finkinstalled = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstalled:
self.t.insert(END, line)
self.update()
self.t.see(END)

threaded:

def insertDump(self):
try:
data = self.dataQueue.get(block=False)
for line in data:
self.t.insert(END, line)
self.t.see(END)
self.update()
except:
print "error"
raise

def getDump(self):

self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue.put(self.file)

Sorry!
--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Mar 27 '07 #2
On Mar 27, 9:07 am, Kevin Walzer <k...@codebykevin.comwrote:
Kevin Walzer wrote:
I'm trying to decide whether I need threads in my Tkinter application or
not. My app is a front end to a command-line tool; it feeds commands to
the command-line program, then reads its output and displays it in a
Tkinter text widget. Some of the commands are long-running and/or return
thousands of lines of output.
I initially thought I needed to use threading, because the GUI would
block when reading the output, even when I configured the blocking to be
non-blocking. I got threading to work, but it seemed a bit complicated.
So, I decided to try something simpler, by using the Tkinter event loop
to force the output to update/display.
it seems to work well enough. Here is my threaded code:
non-threaded:
def insertDump(self):
self.finkinstalled = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstalled:
self.t.insert(END, line)
self.update()
self.t.see(END)
And here is my non-threaded code (needs two functions to work)
def insertDump(self):
try:
data = self.dataQueue.get(block=False)
for line in data:
self.t.insert(END, line)
self.t.see(END)
self.update()
except:
print "error"
raise
def getDump(self):
self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue.put(self.file)
This brings me to a design, as opposed to coding, question. The
non-threaded version seems to work just as well as the threaded one, in
terms of speed. Moreover, it is simpler to code and debug, because I
don't have to check to make sure the thread queue has data (I sometimes
get an 'Empty' error message when I first start the thread). Simply
using the Tk event loop (self.update) is also how I would have coded
this in Tcl.
So my question is this: under what circumstances in Python are threads
considered "best practice"? Am I wrong to use the Tk event loop instead
of threads?

D'oh, I got the code snippets mixed up:

non-threaded:

def insertDump(self):
self.finkinstalled = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
for line in self.finkinstalled:
self.t.insert(END, line)
self.update()
self.t.see(END)

threaded:

def insertDump(self):
try:
data = self.dataQueue.get(block=False)
for line in data:
self.t.insert(END, line)
self.t.see(END)
self.update()

except:
print "error"
raise

def getDump(self):

self.file = os.popen('/sw/bin/fink list', 'r', os.O_NONBLOCK)
self.dataQueue.put(self.file)

Sorry!
--
Kevin Walzer
Code by Kevinhttp://www.codebykevin.com
It looks like Tkinter is similar to wxPython in that you're not
supposed to use the mainloop for anything except the GUI and GUI
commands. The following websites have more info on Tkinter and
threads:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/82965
http://www.thescripts.com/forum/thread22536.html
http://forums.devshed.com/python-pro...ds-123001.html

I use the Threading module for threading in wxPython. I think that
would probably serve you well with Tkinter as well. You can use the
join() method to wait for all the threads to exit.

Mike

Mar 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: python-list | last post by:
Hello, I posted this to the tutor list, but didn't get any responses, unless my email client really messed up. So I'll try here. I'm starting to work with threads, but I'm a little confused. I...
5
by: george.trojan | last post by:
My application consists of Tkinter GUI that has to communicate with a remote server. The communication is bi-directional: the GUI responds to remote requests and user actions uch as pressing a...
3
by: Bob Greschke | last post by:
I have a program where the user pushes a button, a "starting" message is ..inserted to a text field with an associated scroll bar, a thread is started that inserts a "working..." message on to the...
2
by: k2riddim | last post by:
Hello, I'm a beginner with Python and Tkinter development. My application parse links in an html file. And I use Tkinter to implement a GUI. This GUI has a button to launch the parse treatment,...
2
by: Grooooops | last post by:
I've been hacking around this for a few days and have gotten close to what I want... but not quite... The TKinter Docs provide this example: # configure text tag text.tag_config("a",...
9
by: perchef | last post by:
Hi, I have several files to download and a GUI to update. I know this is a frequently asked question but i can't find an appropriate solution. My Downloader extends threading.Thread and update a...
0
by: Dale Huffman | last post by:
There have been a number of posts about calling gui methods from other threads. Eric Brunel. has reccommended calling the gui's ..event_generate method with data passed thru a queue. This worked...
1
by: vigacmoe | last post by:
Hi all, I'm trying to write a simple tkinter program, then this problem popped up. The followin code will describe the problem. ------------------------------------------ import Tkinter ...
7
by: gordon | last post by:
is it possible to send a message to the gui instance while the Tk event loop is running?I mean after i create a gui object like root=Tk() mygui=SomeUI(root) and call root.mainloop() can i...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.