Use threads or Tkinter event loop?

Discussion in 'Python' started by Kevin Walzer, Mar 27, 2007.

  1. Kevin Walzer

    Kevin Walzer Guest

    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
     
    Kevin Walzer, Mar 27, 2007
    #1
    1. Advertising

  2. Kevin Walzer

    Kevin Walzer Guest

    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
     
    Kevin Walzer, Mar 27, 2007
    #2
    1. Advertising

  3. Kevin Walzer

    Guest

    On Mar 27, 9:07 am, Kevin Walzer <> wrote:
    > 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/Cookbook/Python/Recipe/82965
    http://www.thescripts.com/forum/thread22536.html
    http://forums.devshed.com/python-programming-11/tkinter-threads-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, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Usenet Poster!!!
    Replies:
    4
    Views:
    1,867
    Eric Sosman
    Sep 30, 2004
  2. wang xiaoyu
    Replies:
    0
    Views:
    872
    wang xiaoyu
    Jun 25, 2003
  3. gordon

    Tkinter event loop question

    gordon, Aug 27, 2008, in forum: Python
    Replies:
    4
    Views:
    327
    gordon
    Aug 30, 2008
  4. Une bévue
    Replies:
    0
    Views:
    176
    Une bévue
    Jun 14, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    405
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page