A thread import problem

Bruce Sherwood bruce.sherwood at gmail.com
Tue Jul 24 05:33:09 CEST 2012

I'm happy to report that Robin Dunn, the developer of wxPython, showed
me how to solve my VPython architectural problem, using wxPython. I
attach a test program based on wxPython that has all of the properties
I was looking for (though it needs some minor cleanups, including
quitting gracefully, and handling timing better). The attached program
userpoll.py imports the wxpython-based module wxpoll.py which does the

Creates a wxpython environment but does not start the interact loop.
Reads the source of the user program.
   Finds the import of the module and adds imported entities to
globals used with exec.
   Comments out this import statement in the source.
   Does an exec of the modified source, in the primary thread.
   Because the exec is not done in a secondary thread, there are no problems
     associated with other imports such as math that may be in the user program.

The exec-ed program has a loop in it, containing a rate statement
which clamps to no more than 100 iterations per second (to make the
animation run at a reasonable speed) and which calls a one-shot
interact function in the import module. In other words, polling is
used rather than using an interact loop, thereby avoiding the nearly
intractable threading problems. Happily, Robin found a way to make
this polling work with Cocoa on the Mac. I can now move forward with
implementing this machinery for VPython, to be able to run VPython
programs on 64-bit Pythons on the Mac. Hurray!

You will find massive violations of "good practice" in the attached
module, because in order to pull this off I have to do lots of
calculations at the top level of the module. However, I believe the
structure is in fact entirely reasonable and a good solution to a very
tricky problem.

Thanks to those of you who have made suggestions in this forum that
fed into finding a solution. I'll put in a big plug for wxPython
(wxpython.org), which is a great way to do cross-platform GUI
development with Python.

Bruce Sherwood
-------------- next part --------------
A non-text attachment was scrubbed...
Name: userpoll.py
Type: application/octet-stream
Size: 208 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20120723/f88678fe/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: wxpoll.py
Type: application/octet-stream
Size: 8171 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20120723/f88678fe/attachment-0001.obj>

More information about the Python-list mailing list