[wxPython] what is the main thread from the wx point of view?

I'm embedding Python inside Barry's Emacs. Now I'm trying to get wxPython working from Python
inside Barry's Emacs. Now that I call PySys_SetArgv wxPython is no longer crashing and I can
load an test wx program.

I need to run the wx program in a thread, to prevent the Emacs from hanging. But I get this
assert:

0:01:35: ..\msw\thread.cpp(1169): assert failed: only main thread may call wxMutexGuiLeaveOrEnter()!

What is special about the wx main thread and how can I tell wxPython to use a thread I designate?

The threading in Barry's Emacs is typically:

Thread Use
------ ---
main: Barry's Emacs
second: COM server
third: processor for I/O from pipes to Python thread
fourth: Embedded Python (this is python's main thread)
fifth: wxPython app

    Barry

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

I'm embedding Python inside Barry's Emacs.

Okay, I'll bite. What's Barry's Emacs and where can I find out more about
it. From the juicy tidbits you give below it certainly sounds interesting.

Now I'm trying to get wxPython working from Python
inside Barry's Emacs. Now that I call PySys_SetArgv wxPython is no
longer crashing and I can load an test wx program.

Yep, the startup code expects sys.argv to exist, although I don't think it
necessarily needs to have any content does it?

I need to run the wx program in a thread, to prevent the Emacs from
hanging. But I get this assert:

0:01:35: ..\msw\thread.cpp(1169): assert failed: only main thread may call

wxMutexGuiLeaveOrEnter()!

What is special about the wx main thread and how can I tell wxPython to
use a thread I designate?

Starting around 2.2 wxWindows tries to enforce the
only-one-thread-runs-the-gui rule to help prevent errors due to non MT safe
platform libraries. So it has some mutexes and critical sections used at
various points to facilitate that, and there are some wxASSERT checks to
ensure the expected thread is the current one.

The "main thread" is set to whatever is the current thread when wxWindows
initializes itself. In Python we can make the gui thread be other than the
main thread simply by importing wxPython in the context of the desired
thread. As long as wxWindows hasn't already initialized itself in your
process then that thread should be what it considers the "main thread." For
a quickie hack/example:

from time import sleep
from threading import Thread

def test_a_window():
    print "starting window thread"

    from wxPython.wx import * # <-- the wxWin DLL is not loaded until
here

    app = wxPySimpleApp()
    frame = wxFrame(None, -1, "Hello", size=(400,200))
    frame.Show(true)
    app.MainLoop()
    print "finishing window thread"

keep_going = 1
def counter():
    print "starting counter thread"
    count = 0
    while keep_going:
        sleep(1)
        count += 1
        print count
    print "finishing counter thread"

def main():
    print "main startup"

    ct = Thread(target=counter)
    wt = Thread(target=test_a_window)

    ct.start()
    wt.start()
    wt.join()

    global keep_going
    keep_going = 0

    print "main finished"

main()

If this sample doesn't work for you then please let me know. My workspace
has a version of helpers.cpp that handles the python thread state and
interpreter lock a little differently than the released version as I'm
experimenting a little. The above code does work for me (on win2k) so if
the released version doesn't then I'll definitly switch to the new way.

The threading in Barry's Emacs is typically:

Thread Use
------ ---
main: Barry's Emacs
second: COM server
third: processor for I/O from pipes to Python thread
fourth: Embedded Python (this is python's main thread)
fifth: wxPython app

Definitly sounds interesting! Tell us more!

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users