Re: A thread import problem

Discussion in 'Python' started by Bruce Sherwood, Jul 21, 2012.

  1. On Sat, Jul 21, 2012 at 2:53 PM, Dave Angel <> wrote:
    > On 07/21/2012 04:36 PM, Bruce Sherwood wrote:
    >> Thanks much for this clear statement. I hadn't managed to find any
    >> documentation on this specific issue.
    >>
    >> Bruce Sherwood
    >>
    >> On Sat, Jul 21, 2012 at 10:26 AM, Dave Angel <> wrote:
    >>> Two of the things you mustn't do during an import:
    >>>
    >>> 1) start or end any threads
    >>> 2) import something that's already in the chain of pending imports.
    >>> (otherwise known as recursive imports, or import loop). And there's a
    >>> special whammy reserved for those who import the script as though it
    >>> were a module.
    >>>
    >>> Like any rule, there are possible exceptions. But you're much better
    >>> off factoring your code better.
    >>>
    >>> I haven't managed to understand your software description, so i'm not
    >>> making a specific suggestion. But I know others have pointed out that
    >>> you should do as little as possible in top-level code of an imported
    >>> module. Make the work happen in a function, and call that function from
    >>> the original script, not from inside some import. An imported module's
    >>> top-level code should do nothing more complex than initialize module
    >>> constants.
    >>>
    >>>
    >>> --
    >>>
    >>> DaveA
    >>>
    >>>

    >
    > (You top-posted, which makes it harder to figure out who said what.)
    >
    > For docs on the threading thing, see:
    >
    > http://docs.python.org/library/threading.html
    >
    > " ... an import should not have the side effect of spawning a new thread
    > and then waiting for that thread in any way..."
    >
    >
    >
    >
    > --
    >
    > DaveA
    >


    Thanks. I had read that as forbidding "waiting for that thread", not
    forbidding spawning a new thread. The following sentence says,
    "Failing to abide by this restriction can lead to a deadlock if the
    spawned thread directly or indirectly attempts to import a module." I
    gather that a clearer, more forceful statement might be, "Failing to
    abide by this restriction WILL lead to a deadlock if the spawned
    thread directly or indirectly attempts to import a module." All of
    which implies the behavior I've seen in various experiments, namely
    that as long as the spawned thread doesn't do any imports, I haven't
    seen any problems with spawning a thread in an imported module. I take
    your word for it that this is a no-no, but I don't know why.

    Bruce Sherwood
     
    Bruce Sherwood, Jul 21, 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:
    975
  2. per9000
    Replies:
    7
    Views:
    421
    Magnus Lycka
    Feb 27, 2006
  3. Bruce Sherwood

    A thread import problem

    Bruce Sherwood, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    162
    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:
    184
    Dennis Lee Bieber
    Jul 19, 2012
  5. Dieter Maurer

    Re: A thread import problem

    Dieter Maurer, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    154
    Dieter Maurer
    Jul 19, 2012
Loading...

Share This Page