Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'modify': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'exec': 0.07; 'executable': 0.07; 'problem?': 0.07; '"class"': 0.09; '"if': 0.09; 'level:': 0.09; 'meaningful': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'thread': 0.11; 'source,': 0.15; '"def"': 0.16; '"global"': 0.16; '"if"': 0.16; '"import': 0.16; 'bullet': 0.16; 'constants': 0.16; 'ends,': 0.16; 'give.': 0.16; 'invoking': 0.16; 'lhs': 0.16; 'polling': 0.16; 'prog': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'rhs': 0.16; 'run(self):': 0.16; 'scope.': 0.16; 'structures,': 0.16; 'subject:import': 0.16; 'threading': 0.16; 'trail.': 0.16; 'true:': 0.16; 'value;': 0.16; 'later': 0.16; 'wed,': 0.16; 'fix': 0.17; 'basically': 0.17; 'exists': 0.17; 'tries': 0.17; 'module': 0.19; 'discussion': 0.20; 'math': 0.20; 'versions': 0.20; 'define': 0.20; 'suggested': 0.20; 'import': 0.21; 'assignment': 0.22; 'subject:problem': 0.22; 'runs': 0.22; 'work,': 0.22; "i'd": 0.22; 'bruce': 0.23; 'specified': 0.23; 'statement': 0.23; "i've": 0.23; 'raise': 0.24; 'tried': 0.25; 'environment.': 0.27; 'execution': 0.27; 'replace': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'lines': 0.28; 'block,': 0.29; 'cpu': 0.29; 'forces': 0.29; 'identifies': 0.29; 'invoke': 0.29; 'loop,': 0.29; 'sleep': 0.29; 'wrap': 0.29; 'definition': 0.29; 'no,': 0.29; 'class': 0.29; 'call.': 0.30; 'waste': 0.30; 'error': 0.30; 'code': 0.31; 'problem.': 0.32; 'url:python': 0.32; 'file': 0.32; 'message.': 0.33; 'url:home': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'skip:- 20': 0.34; 'thanks': 0.34; 'false': 0.35; 'path': 0.35; 'there': 0.35; 'add': 0.36; 'received:org': 0.36; 'explain': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.36; 'charset:us- ascii': 0.36; 'skip:p 20': 0.36; 'does': 0.37; 'level': 0.37; 'being': 0.37; 'why': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'comment': 0.38; 'skip:o 20': 0.38; 'url:docs': 0.38; 'advice': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'side': 0.61; 'respect': 0.63; 'success': 0.63; 'more': 0.63; 'jul': 0.65; 'started.': 0.65; 'saw': 0.75; '(your': 0.84; 'threading"': 0.84; 'dennis': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: A thread import problem Date: Wed, 18 Jul 2012 20:47:26 -0400 Organization: > Bestiaria Support Staff < References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-253-106-193.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list 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: 118 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342658854 news.xs4all.nl 6978 [2001:888:2000:d::a6]:32932 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25596 On Wed, 18 Jul 2012 17:03:52 -0600, Bruce Sherwood declaimed the following in gmane.comp.python.general: > > 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. > "global" is a false trail. "global" is only meaningful inside a "def" block, where it identifies the specified name to be module level scope. Otherwise any assignment statement in the block with the name on the left side forces the name to be a local name. Note that in the absence of a LHS assignment, you don't need "global" -- RHS will follow the path of "is local: yes, retrieve local value; no, is in the module level: yes, retrieve module level value; no, raise exception" > Can anyone explain why the math import statement causes a problem? > Thanks for any advice you can give. > > Bruce Sherwood > > --------------------------- > The main program: > > from import_test import * > print('exec this file') > global math > from math import sin > print(sin(3.14159/6)) > > ----------------------------- > Contents of import_test: > > from threading import Thread > from time import sleep > import sys > > prog = open(sys.argv[0]).read() > prog = '#'+prog # comment out the import statement > print(prog) > > class worker(Thread): > def run(self): > print('start thread') > exec(prog) > > w = worker() > w.start() > > while True: > sleep(1) Given that this is an endless polling loop, you might as well replace it with w.join() which has the advantage of breaking out of execution if the thread ends, and doesn't waste CPU time on a sleep call. Given that your scheme requires being able to modify the program to invoke your "runner" module in the first place, I'd be looking for a way to have the program invoke your thread directly... That would be to put the work inside a "def work(...)", add an "if __name__ == "__main__": #spawn a thread invoking work() -=-=-=- import threading def theWork(): import math print(math.sin(3.14159/6)) if __name__ == "__main__": wrkr = threading.Thread(target=theWork, args=(), kwargs={}) wrkr.start() wrkr.join() -=-=-=- Basically wrap the file with the "import threading" and "if __name__ == ..." If the program already has an "if __name__ ..." block at the bottom, change the "if" to a "def worker():", otherwise wrap any naked executable lines (anything that does more than definition of constants or scope level global data structures, "class" or "def" statements... PI = 3.1415926536 #though math.pi exists so why define your own class Something(object): ... def something(): ... are okay... if sys.argv[1] == "debug": some-statement else: other-statement should be inside a "def worker()" so that the code only runs when the worker thread is started. Secondly: read the first bullet under http://docs.python.org/library/threading.html#importing-in-threaded-code (Your import is spawning a thread which then tries to perform an import) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/