[wxPython] thread problem in wxPython demo? (more)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] thread problem in wxPython demo? (more)

Bob Klimek-2
Hello,

A bit more info on the thread problem. I re-booted my machine and for
some reason it almost fixed the message ("GLib-CRITICAL **: file
gmain.c:
line 500 (g_source_remove): assertion 'tag > 0' failed") which I noted
in my last post. I still get it, but a lot less than yesterday. I could
be imagining things but it seems that the messages come up more when I
move the mouse, but not in any reproducible way and not every time.

Hoping to try to understand the problem, I stripped down the thread
demo. Here it is attached.

Bob
# Program Threads2. This is a simplified wxPython demo program (thread demo)

from wxPython.wx import *
import thread
import time
from   whrandom import random


wxEVT_UPDATE_BARGRAPH = 25015

# Associates the event with a callback function (is called from frame)
def EVT_UPDATE_BARGRAPH(win, func):
    win.Connect(-1, -1, wxEVT_UPDATE_BARGRAPH, func)


class UpdateBarEvent(wxPyEvent): # event handler?
    def __init__(self, value):
        wxPyEvent.__init__(self)
        self.SetEventType(wxEVT_UPDATE_BARGRAPH)
        self.value = value

#----------------------------------------------------------------------

class CalcBarThread:
    def __init__(self, win, val):
        self.win = win
        self.val = val

    def Start(self):
        self.keepGoing = true
        thread.start_new_thread(self.Run, ())

    def Stop(self):
        self.keepGoing = false

    def Run(self):
        while self.keepGoing:
            evt = UpdateBarEvent(int(self.val))
            wxPostEvent(self.win, evt)
            #del evt

            sleeptime = (random() * 2) + 0.5
            time.sleep(sleeptime)
            sleeptime = sleeptime * 5
            if int(random() * 2):
                self.val = self.val + sleeptime
            else:
                self.val = self.val - sleeptime
            if self.val < 0: self.val = 0
            if self.val > 300: self.val = 300

#----------------------------------------------------------------------

class GraphWindow(wxWindow):
    def __init__(self, parent):
        wxWindow.__init__(self, parent, -1)
        self.value = 0

    def SetValue(self, value):
        self.value = value

    def OnPaint(self, evt):
        size = self.GetSize()
        bmp = wxEmptyBitmap(size.width, size.height)
        dc = wxMemoryDC()
        dc.SelectObject(bmp)
        color = wxRED
        dc.SetPen(wxPen(color))
        dc.SetBrush(wxBrush(color))
        dc.DrawRectangle(100, 10, self.value, 10)

        wdc = wxPaintDC(self)
        wdc.BeginDrawing()
        wdc.Blit(0,0, size.width, size.height, dc, 0,0)
        wdc.EndDrawing()

#----------------------------------------------------------------------

class TestFrame(wxFrame):
    def __init__(self, parent, log):
        wxFrame.__init__(self, parent, -1, "Thread Test", size=(450,300))

        panel = wxPanel(self, -1)
        self.graph = GraphWindow(self)

        sizer = wxBoxSizer(wxVERTICAL)
        sizer.Add(panel, 0, wxEXPAND)
        sizer.Add(self.graph, 1, wxEXPAND)
        self.SetSizer(sizer)
        self.SetAutoLayout(true)

        EVT_UPDATE_BARGRAPH(self, self.updateBar) # associate callback function
        self.th = CalcBarThread(self, 50)  # get pointer to thread class
        self.th.Start()  # start thread

    # callback function - is called automatically when event occurs
    def updateBar(self, evt):
        self.graph.SetValue(evt.value)
        print 'value=%d' % (evt.value)
        self.graph.Refresh(false)

    def OnCloseWindow(self, evt):
        self.th.Stop()
        self.Destroy()

#--------------------------------------

class mtApp(wxApp):
    def OnInit(self):
        frame = TestFrame(NULL, -1)
        frame.Show(true)
        self.SetTopWindow(frame)
        return true


app = mtApp(0)
app.MainLoop()





##
##
##overview = """\
##The main issue with multi-threaded GUI programming is the thread safty
##of the GUI itself.  On most platforms the GUI is not thread safe and
##so any cross platform GUI Toolkit and applications written with it
##need to take that into account.
##
##The solution is to only allow interaction with the GUI from a single
##thread, but this often severly limits what can be done in an
##application and makes it difficult to use additional threads at all.
##
##Since wxPython already makes extensive use of event handlers, it is a
##logical extension to allow events to be sent to GUI objects from
##alternate threads.  A function called wxPostEvent allows you to do
##this.  It accepts an event and an event handler (window) and instead
##of sending the event immediately in the current context like
##ProcessEvent does, it processes it later from the context of the GUI
##thread.
##
##"""
Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] thread problem in wxPython demo? (more)

Robin Dunn
> Hello,
>
> A bit more info on the thread problem. I re-booted my machine and for
> some reason it almost fixed the message ("GLib-CRITICAL **: file
> gmain.c:
> line 500 (g_source_remove): assertion 'tag > 0' failed") which I noted
> in my last post. I still get it, but a lot less than yesterday. I could
> be imagining things but it seems that the messages come up more when I
> move the mouse, but not in any reproducible way and not every time.
>

I wasn't able to reproduce this problem with my current workspace, odds are
that it was a bug in wxGTK that has since been fixed.

On the other hand, I was able to reproduce it on a different machine using
the 2.1.11 RPM's and gtk/glib 1.2.3, but only with your sample and not the
wxPython demo...  So I looked at what might be different between the two.
Don't ask me why but your sample code won't get the glib warning message if
you add these lines to your OnPaint after the dc.SelectObject(bmp):

        dc.SetBackground(wxBrush(self.GetBackgroundColour()))
        dc.Clear()

I also noticed that it only happens the second time the message is sent from
the background thread, which is the 3rd paint event, so I'm thinking that
maybe there is some problem with cleaning up after itself on the previous
event.  The I tried removing the two lines above and adding this at the end
of OnPaint:

        dc.SelectObject(wxNullBitmap)

and that takes care of the problem too.

--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] thread problem in wxPython demo? (more)

Bob Klimek-2
Robin Dunn wrote:

>
> > Hello,
> >
> > A bit more info on the thread problem. I re-booted my machine and for
> > some reason it almost fixed the message ("GLib-CRITICAL **: file
> > gmain.c:
> > line 500 (g_source_remove): assertion 'tag > 0' failed") which I noted
> > in my last post. I still get it, but a lot less than yesterday. I could
> > be imagining things but it seems that the messages come up more when I
> > move the mouse, but not in any reproducible way and not every time.
> >
>
> I wasn't able to reproduce this problem with my current workspace, odds are
> that it was a bug in wxGTK that has since been fixed.
>

I don't think that's right. I went out and got the latest libraries I
could find and its still happening. I use RH 6.1 with KDE. The RPM's are
as follows. Is there anything later?
gtk+-1.2.6-1
glib-1.2.6-2
wxGTK-2.1.11-0
wxPython-2.1.11-1

 

> On the other hand, I was able to reproduce it on a different machine using
> the 2.1.11 RPM's and gtk/glib 1.2.3, but only with your sample and not the
> wxPython demo...  So I looked at what might be different between the two.
> Don't ask me why but your sample code won't get the glib warning message if
> you add these lines to your OnPaint after the dc.SelectObject(bmp):
>
>         dc.SetBackground(wxBrush(self.GetBackgroundColour()))
>         dc.Clear()
>
> I also noticed that it only happens the second time the message is sent from
> the background thread, which is the 3rd paint event, so I'm thinking that
> maybe there is some problem with cleaning up after itself on the previous
> event.  The I tried removing the two lines above and adding this at the end
> of OnPaint:
>
>         dc.SelectObject(wxNullBitmap)
>
> and that takes care of the problem too.
>

Neither one of these worked for me.
If anything else comes to mind, please let me know.

Bob

_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] thread problem in wxPython demo? (more)

Robin Dunn
>
> I don't think that's right. I went out and got the latest libraries I
> could find and its still happening. I use RH 6.1 with KDE. The RPM's are
> as follows. Is there anything later?
> gtk+-1.2.6-1
> glib-1.2.6-2
> wxGTK-2.1.11-0
> wxPython-2.1.11-1
>

The pre-release of 2.1.12 currently in CVS, which is what is in my worksapce.
(See http://web.ukonline.co.uk/julian.smart/wxwin/cvs.htm for annonymous
access.)

Or the daily snapshot at
http://www.lpthe.jussieu.fr/~zeitlin/wxWindows/daily/results.html


--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!




_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] thread problem in wxPython demo? (more)

Jody Winston
In reply to this post by Bob Klimek-2
I'm having simular problems on an SGI on IRIX 6.5 using:
 o Python 1.5.2
 o compiled with -o32 using SGI's compilers
 o glib-1.2.6
 o gtkglarea-1.2.1
 o wxPython-2.1.11
 o glade-0.5.3
 o gtk+-1.2.6
 o wxGTK-2.1.11

Running the demo "Threads".  Running the program without a debugger
produces the following message:

Fatal Python error: ceval: tstate mix-up

using a debugger:

GLib-CRITICAL **: file gmain.c: line 500: assertion `tag > 0' failed.
Fatal Python error: PyThreadState_Get: no current thread
Process  3333 (python) stopped on signal SIGABRT: Abort (see abort(3c)) (default) in _prctl at line 15 in file "prctl.s"
         Source (of prctl.s) not available for Process  3333
(dbx) where
>  0 _prctl(0x15, 0x4, 0xffff, 0x0) ["prctl.s":15, 0xfa35450]
   1 pthread_kill(0x15, 0x6, 0x0, 0x20) ["sig.c":135, 0xc2b4b3c]
   2 _SGIPT_ptsig_raise(0x6, 0x4, 0xffff, 0x0) ["sig.c":625, 0xc2b5e88]
   3 _raise(0x15, 0x4, 0xffff, 0x0) ["raise.c":26, 0xfac91ec]
   4 abort(0x15, 0x4, 0xffff, 0x0) ["abort.c":52, 0xfa5d9b4]
   5 Py_FatalError(0x15, 0x4, 0xffff, 0x0) ["../../Python/pythonrun.c":1036, 0x42752c]
   6 PyThreadState_Get(0x15, 0x4, 0xffff, 0x0) ["../../Python/pystate.c":204, 0x427fc8]
   7 PyErr_Occurred(0x15, 0x4, 0xffff, 0x0) ["../../Python/errors.c":113, 0x43d2dc]
   8 convertsimple1(0x102b9ee8, 0x103a3ae0, 0xffff, 0x0) ["../../Python/getargs.c":534, 0x439e8c]
   9 convertsimple(0x102b9ee8, 0x4, 0xffff, 0x103a3be0) ["../../Python/getargs.c":435, 0x439adc]
   10 convertitem(0x15, 0x103a3ce4, 0xffff, 0x103a3b60) ["../../Python/getargs.c":415, 0x439a78]
   11 vgetargs1(0x102b9ee8, 0x58d3008, 0x103a3d00, 0x1) ["../../Python/getargs.c":212, 0x4392a0]
   12 PyArg_Parse(0x15, 0x58d3008, 0x103a3d28, 0x102b9ee8) ["../../Python/getargs.c":86, 0x438f78]
   13 time_sleep(0x15, 0x4, 0xffff, 0x0) ["../../Modules/timemodule.c":237, 0x58c1338]
   14 call_builtin(0x102a64a8, 0x102bda18, 0x0, 0x102bda18) ["../../Python/ceval.c":2359, 0x42dbe4]
   15 PyEval_CallObjectWithKeywords(0x15, 0x102bda18, 0x0, 0x102a6478) ["../../Python/ceval.c":2324, 0x42da04]
   16 eval_code2(0x102ba128, 0x102ba8e0, 0x0, 0x102bda0c) ["../../Python/ceval.c":1654, 0x42bdb0]
   17 call_function(0x102c68c0, 0x102bda00, 0x0, 0x102bd928) ["../../Python/ceval.c":2484, 0x42e22c]
   18 PyEval_CallObjectWithKeywords(0x15, 0x1001ff18, 0x0, 0x102dfa60) ["../../Python/ceval.c":2322, 0x42d9dc]
   19 t_bootstrap(0x102bd928, 0x4, 0xffff, 0x0) ["../../Modules/threadmodule.c":224, 0x46e3ac]
   20 _SGIPT_pt_start() ["pt.c":791, 0xc2b1ef8]
(dbx)

Another message:

Fatal Python error: ceval: tstate mix-up
Process  3336 (python) stopped on signal SIGABRT: Abort (see abort(3c)) (default) in _prctl at line 15 in file "prctl.s"
         Source (of prctl.s) not available for Process  3336
(dbx) where
>  0 _prctl(0x15, 0x4, 0xffff, 0x0) ["prctl.s":15, 0xfa35450]
   1 pthread_kill(0x15, 0x6, 0x0, 0x20) ["sig.c":135, 0xc2b4b3c]
   2 _SGIPT_ptsig_raise(0x6, 0x4, 0xffff, 0x0) ["sig.c":625, 0xc2b5e88]
   3 _raise(0x15, 0x4, 0xffff, 0x0) ["raise.c":26, 0xfac91ec]
   4 abort(0x15, 0x4, 0xffff, 0x0) ["abort.c":52, 0xfa5d9b4]
   5 Py_FatalError(0x15, 0x4, 0xffff, 0x0) ["../../Python/pythonrun.c":1036, 0x42752c]
   6 eval_code2(0x102b9f68, 0x102ba720, 0x0, 0x102bd3b4) ["../../Python/ceval.c":626, 0x429094]
   7 call_function(0x102c68c0, 0x102bd3a8, 0x0, 0x102dffa0) ["../../Python/ceval.c":2484, 0x42e22c]
   8 PyEval_CallObjectWithKeywords(0x15, 0x1001ff18, 0x0, 0x102dffa8) ["../../Python/ceval.c":2322, 0x42d9dc]
   9 t_bootstrap(0x102bd330, 0x4, 0xffff, 0x0) ["../../Modules/threadmodule.c":224, 0x46e3ac]
   10 _SGIPT_pt_start() ["pt.c":791, 0xc2b1ef8]
(dbx)




_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users