Strange threading behaviour

inq1ltd inq1ltd at inqvista.com
Thu Jun 21 17:46:30 CEST 2012


On Thursday, June 21, 2012 04:19:41 PM Rotwang wrote:
> Hi all, I'm using Python 2.7.2 on Windows 7 and a module I've written is
> acting strangely. I can reproduce the behaviour in question with the
> following:
> 
> --- begin bugtest.py ---
> 
> import threading, Tkinter, os, pickle

try this;

from Tkinter import *

take it out of the other import line

jd 



> 
> class savethread(threading.Thread):
>      def __init__(self, value):
>          threading.Thread.__init__(self)
>          self.value = value
>      def run(self):
>          print 'Saving:',
>          with open(os.path.join(os.getcwd(), 'bugfile'), 'wb') as f:
>              pickle.dump(self.value, f)
>          print 'saved'
> 
> class myclass(object):
>      def gui(self):
>          root = Tkinter.Tk()
>          root.grid()
>          def save(event):
>              savethread(self).start()
>          root.bind('s', save)
>          root.wait_window()
> 
> m = myclass()
> m.gui()
> 
> --- end bugtest.py ---
> 
> 
> Here's the problem: suppose I fire up Python and type
> 
>  >>> import bugtest
> 
> and then click on the Tk window that spawns and press 's'. Then
> 'Saving:' gets printed, and an empty file named 'bugfile' appears in my
> current working directory. But nothing else happens until I close the Tk
> window; as soon as I do so the file is written to and 'saved' gets
> printed. If I subsequently type
> 
>  >>> bugtest.m.gui()
> 
> and then click on the resulting window and press 's', then 'Saving:
> saved' gets printed and the file is written to immediately, exactly as I
> would expect. Similarly if I remove the call to m.gui from the module
> and just call it myself after importing then it all works fine. But it
> seems as if calling the gui within the module itself somehow stops
> savethread(self).run from finishing its job while the gui is still alive.
> 
> Can anyone help?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120621/1dc90a2b/attachment-0001.html>


More information about the Python-list mailing list