Created
December 11, 2012 03:59
-
-
Save carlohamalainen/4255784 to your computer and use it in GitHub Desktop.
thread timing issues with CallAfter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Typical failure, about 70% of the time on a Debian Squeeze 32bit VM (in VirtualBox, with Debian Squeeze 64bit host OS). | |
Exception in thread Thread-1: | |
Traceback (most recent call last): | |
File "/opt/sw/32bit/debian/python/2.7.3/lib/python2.7/threading.py", line 551, in __bootstrap_inner | |
self.run() | |
File "blammo.py", line 49, in run | |
myappMainFrame.loginThread.boo = 42 | |
AttributeError: 'MyAppMainFrame' object has no attribute 'loginThread' | |
""" | |
import sys | |
import wx | |
import threading | |
import os | |
import time | |
global myappMainFrame | |
myappMainFrame = None | |
class MyAppMainFrame(wx.Frame): | |
def __init__(self, parent, id, title): | |
wx.Frame.__init__(self, parent, id, title, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER) | |
self.loginDialogPanel = wx.Panel(self, wx.ID_ANY) | |
self.loginDialogPanelSizer = wx.FlexGridSizer(rows=2, cols=1, vgap=15, hgap=5) | |
self.buttonsPanel = wx.Panel(self.loginDialogPanel, wx.ID_ANY) | |
self.buttonsPanelSizer = wx.FlexGridSizer(rows=1, cols=3, vgap=5, hgap=10) | |
self.buttonsPanel.SetSizer(self.buttonsPanelSizer) | |
LOGIN_BUTTON_ID = 1 | |
self.loginButton = wx.Button(self.buttonsPanel, LOGIN_BUTTON_ID, 'Login') | |
self.buttonsPanelSizer.Add(self.loginButton, flag=wx.TOP|wx.BOTTOM|wx.LEFT|wx.RIGHT, border=10) | |
self.Bind(wx.EVT_BUTTON, self.onLogin, id=LOGIN_BUTTON_ID) | |
self.buttonsPanel.SetSizerAndFit(self.buttonsPanelSizer) | |
self.loginDialogPanelSizer.Add(self.buttonsPanel, flag=wx.ALIGN_RIGHT|wx.BOTTOM|wx.LEFT|wx.RIGHT, border=15) | |
self.loginButton.SetDefault() | |
self.loginDialogPanel.SetSizerAndFit(self.loginDialogPanelSizer) | |
self.loginDialogPanel.Layout() | |
self.Fit() | |
self.Layout() | |
self.Centre() | |
def onLogin(self, event): | |
class LoginThread(threading.Thread): | |
def __init__(self, notify_window): | |
threading.Thread.__init__(self) | |
self._notify_window = notify_window | |
self.start() | |
def do_something(self): | |
print 'in do_something' | |
return | |
def run(self): | |
# Comment out this time.sleep call and everything's ok | |
# time.sleep(1) | |
myappMainFrame.loginThread.boo = 42 | |
wx.CallAfter(self.do_something) | |
os._exit(0) | |
self.loginThread = LoginThread(self) | |
class MyApp(wx.App): | |
def OnInit(self): | |
global myappMainFrame | |
myappMainFrame = MyAppMainFrame(None, wx.ID_ANY, ' ') | |
myappMainFrame.Show(True) | |
return True | |
app = MyApp(False) | |
app.MainLoop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment