Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25750

Re: A thread import problem

Date 2012-07-21 12:26 -0400
From Dave Angel <d@davea.name>
Subject Re: A thread import problem
References <CA+WuaScBDU_5LcrCrTtDr6N3gHCk9hQ03fA=DT359GCRY=a=KQ@mail.gmail.com> <CA+WuaSeRExxaD9yCNWhXoWqsV_ZqP8DOvdErMRdNS-H_UtzQyg@mail.gmail.com> <87a9yt7bw6.fsf@handshake.de> <CA+WuaSc1wzUqUyw_=QqryN2H_FKYqxspnuqBWECVq5-DH2Lcvg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2388.1342888031.4697.python-list@python.org> (permalink)

Show all headers | View raw


On 07/21/2012 10:54 AM, Bruce Sherwood wrote:
> Thanks much for this suggestion. I'm not sure I've correctly
> understood the operation "start_new_thread(lambda: __import__(<your
> module>), ())". By "your module" do you mean the user program which
> imported the module that will execute start_new_thread? It hadn't
> occurred to me to have A import B and B import A, though now that you
> describe this (if that's indeed what you mean) it makes sense. The
> original instance of A won't get past its initial import statement
> because the main loop won't return to it.
>
> Bruce Sherwood
>

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: A thread import problem Dave Angel <d@davea.name> - 2012-07-21 12:26 -0400

csiph-web