Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'initialize': 0.05; 'function,': 0.07; 'constants.': 0.09; 'imported': 0.09; 'script,': 0.09; 'cc:addr:python-list': 0.10; '"your': 0.16; 'exceptions.': 0.16; "module's": 0.16; 'subject:import': 0.16; 'suggestion.': 0.16; 'threads': 0.16; 'top-level': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'pointed': 0.17; 'module': 0.19; 'pending': 0.20; 'import': 0.21; 'subject:problem': 0.22; 'cc:2**0': 0.23; 'bruce': 0.23; 'statement': 0.23; "haven't": 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'script': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'skip:" 20': 0.26; 'am,': 0.27; 'module.': 0.27; 'initial': 0.28; 'description,': 0.29; 'occurred': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; 'thanks': 0.34; "won't": 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'should': 0.36; 'possible': 0.37; 'correctly': 0.37; 'execute': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'some': 0.38; 'things': 0.38; 'nothing': 0.38; 'sure': 0.38; 'received:192': 0.39; 'little': 0.39; 'received:192.168': 0.40; 'end': 0.40; 'your': 0.60; 'more': 0.63; 'making': 0.64; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'special': 0.73 Date: Sat, 21 Jul 2012 12:26:35 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Bruce Sherwood Subject: Re: A thread import problem References: <87a9yt7bw6.fsf@handshake.de> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:Fx+PpkmqvPnXkGTaMFCFZXwhANxghec5fdAKUDVOeFQ rNpOCM3em00t9I4IR13XHafzGTOU5fSFq4ZWPUrNJTmAs/Grry DR6I28W8mYRNtXpXdg2ko/F/9gp0qhW05M2lKZ3UwcyEB61laA 6ZIQHHgisvmGJLtsDJbs722GHiDJgm0gX2eIHl/2ntXJmIm+f7 cwoodv0HuWTmTKqusi0VCAt57rhXdaIIsor90YW3hlqZ7tfcme w3ByYB3PzH5sumNGOjo3YL+a/zc2kRS11+9hACDj3xKbDbNRnU C4r+i72TBkqUEdHvtLCrvf0NJjLYe3cGdHDU0RdGSBfdMCfZw= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342888031 news.xs4all.nl 6889 [2001:888:2000:d::a6]:36651 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25750 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__( 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