But I don't suppose it would be too hard to re-arrange so that
the users code was running in the secondary thread.
Alas, no, it's (nearly) impossible. Remember that the VPython API
requires that it be possible for a user to write a program of the
following simple kind:
from visual import *
b = box()
while True:
rate(100)
b.pos.x += .001
I don't see any way for the module import to throw the user's 3-line
program into a secondary thread, unless I create a special program text
editor (based on IDLE, presumably) that rearranges the program
appropriately. However, if someone sees a way to achieve the effect
without a preprocessor, I'm all ears.
Is the above something that the user would expect to be able to run in the plain interactive interpreter? Or is it something that they will save to a file and then run?
If the latter then it would not be difficult to write a loader script which starts the wx application in the main thread and starts the worker thread which then loads the source file and executes it within the thread's context.
If it's supposed to work in an interactive interpreter environment then creating a GUI shell like PyCrust (or using it as a starting point) would allow you to essentially share the MainLoop with the interactive interpreter and the VPython code. As long as there is nothing that totally blocks without yielding then it would probably work ok.
Nor can I imagine somehow writing my own interact loop that is somehow
Cocoa but somehow not Cocoa, somehow getting around the requirement that
the Cocoa interact loop be in the primary thread.
You could call try calling wx.GetApp().Yield() from within whatever is your inner loop. Inside the rate() function I guess. If that doesn't fully work because the real MainLoop hasn't been called yet, then you can probably do the same thing it would do by creating your own instance of an wx.GUIEventLoop, activating it, and calling its Yield or YieldFor methods. I think that will effectively be the same as performing one iteration of the MainLoop.
A different approach, should it prove literally impossible to achieve
the necessary result on a Mac: Is there a way to tell wxPython (or
wxWidgets) to use the Unix/X11 framework instead of Cocoa? In other
words, to do something similar to what is done on Linux? That would of
course have the disadvantage of not looking completely native, but it
would make possible running on 64-bit Python, which is impossible with
the current Carbon implementation of VPython.
It should be possible to use the wxGTK port, but some work would need to be done to accomplish it as the build-wxpython and build-wxwidgets scripts assume that if you are running them on a Mac then you are wanting to build one of the Mac ports. So you would need to do what they are doing (run wx's configure and make, run wxPython's setup.py, etc.) by hand so you can use different options. I expect that wxWidgets will build okay using the --with-gtk configure option, although it may need some tweaks here and there. wxPython's setup.py and config.py will likely need some hacking to make it work. Look for anything inside conditionals based on sys.platform == 'darwin'
···
On 6/26/12 10:24 PM, Bruce Sherwood wrote:
--
Robin Dunn
Software Craftsman