Threading issue

  • I want to make a version of VPython (vpython.org) that is based on
    Cocoa (the Mac version is currently based on Carbon).

    The VPython API permits the following short user program, which
    displays a 3D cube moving to the right, and you can rotate and zoom
    the camera with the mouse:

    from visual import box
    b = box()
    while True:
        b.pos.x += 0.001

    This works because a GUI environment is invoked by the visual module
    in a secondary thread (written mainly in C++, connected to Python by
    Boost). The OpenGL rendering of the box in its current position is
    driven by a 30-millisecond timer. This works with any environment
    other than Cocoa.

    However, the Cocoa GUI environment and interact loop are required to
    be in the primary thread, so the challenge is to have the visual
    module set up the Cocoa environment, with the user's program running
    in a secondary thread.

    One of the things I've tried is to have the visual module read the
    user's program, comment out the import statement, and start a
    secondary thread in which I execute the (modified) user's code using
    the Python exec statement. This has the problem that threads started
    in Python modules apparently cannot execute import statements, so that
    if there are additional import statements in the user's program (such
    as importing math functions), the program halts. I've written code to
    find all import statements in the user's program, execute them to add
    their symbols to the global symbol table, and pass them as the
    environment for the exec statement, but that's a pretty gross kludge.

    Any suggestions?

    Bruce Sherwood
  • On Jul 20, 2012, at 12:04 PM, Bruce Sherwood <bruce.sherwood...> wrote:

    > However, the Cocoa GUI environment and interact loop are required to
    > be in the primary thread, so the challenge is to have the visual
    > module set up the Cocoa environment, with the user's program running
    > in a secondary thread.

    This sounds like a Python embedding question ("how do I start and run the Python interpreter on a background thread?") not a Cocoa question. I don't know if there's anyone here with the necessary Python embedding expertise to answer it; you'd probably have better luck on a Python forum.

    —Jens
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today