Re: A thread import problem

Discussion in 'Python' started by Dieter Maurer, Jul 19, 2012.

  1. Bruce Sherwood <> writes:

    > I'm trying to do something rather tricky, in which a program imports a
    > module that starts a thread that exec's a (possibly altered) copy of
    > the source in the original program, and the module doesn't return.
    > This has to do with an attempt to run VPython in the Mac Cocoa
    > context, in which Cocoa is required to be the primary thread, making
    > it necessary to turn the environment inside out, as currently VPython
    > invokes the Carbon context as a secondary thread.
    >
    > I've created a simple test case, displayed below, that illustrates
    > something I don't understand. The module reads the source of the
    > program that imported it, comments out the import statement in that
    > source, and performs an exec of the modified source. The module then
    > enters an infinite loop, so that there is no return to the original
    > program; only the exec-ed program runs, and it runs in a secondary
    > thread.
    >
    > The puzzle is that if there is any later import statement in the exec
    > source, the exec program halts on that import statement, with no error
    > message. I saw a discussion that suggested a need for the statement
    > "global math" to make the math import work, but that doesn't fix the
    > problem. I've tried with no success various versions of the exec
    > statement, with respect to its global and local environment.


    In a recent discussion in this list someone mentioned that
    on module import, you should not start a thread. The reason: apparently,
    Python uses some kind of locking during import which can interfere
    with "import"s in the started thread.

    You can (in principle) easily avoid starting the thread on module import.
    Instead of starting the thread as a side effect of the import,
    put the start in a function, import the module and then call
    the thread starting function.
     
    Dieter Maurer, Jul 19, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Seefeld
    Replies:
    3
    Views:
    965
  2. per9000
    Replies:
    7
    Views:
    417
    Magnus Lycka
    Feb 27, 2006
  3. Bruce Sherwood

    A thread import problem

    Bruce Sherwood, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    161
    Bruce Sherwood
    Jul 19, 2012
  4. Dennis Lee Bieber

    Re: A thread import problem

    Dennis Lee Bieber, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    182
    Dennis Lee Bieber
    Jul 19, 2012
  5. Bruce Sherwood

    Re: A thread import problem

    Bruce Sherwood, Jul 20, 2012, in forum: Python
    Replies:
    0
    Views:
    157
    Bruce Sherwood
    Jul 20, 2012
Loading...

Share This Page