Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.05; 'modified': 0.05; 'source.': 0.05; 'sys': 0.05; 'exec': 0.07; 'performs': 0.07; 'problem?': 0.07; 'imported': 0.09; 'imports': 0.09; 'received:mail-vc0-f174.google.com': 0.09; 'runs,': 0.09; 'thread,': 0.09; 'def': 0.10; 'thread': 0.11; 'source,': 0.15; 'give.': 0.16; 'illustrates': 0.16; 'invokes': 0.16; 'prog': 0.16; 'run(self):': 0.16; 'subject:import': 0.16; 'thread.': 0.16; 'threading': 0.16; 'true:': 0.16; 'vpython': 0.16; 'later': 0.16; 'fix': 0.17; 'module': 0.19; 'discussion': 0.20; 'math': 0.20; 'versions': 0.20; 'suggested': 0.20; 'trying': 0.21; 'import': 0.21; 'displayed': 0.22; 'subject:problem': 0.22; 'runs': 0.22; 'work,': 0.22; 'bruce': 0.23; 'statement': 0.23; "i've": 0.23; 'tried': 0.25; 'environment.': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'run': 0.28; 'environment': 0.29; '(possibly': 0.29; 'cocoa': 0.29; 'context,': 0.29; 'loop,': 0.29; 'received:209.85.220.174': 0.29; 'sleep': 0.29; 'case,': 0.29; 'starts': 0.29; 'source': 0.29; 'class': 0.29; "i'm": 0.29; 'error': 0.30; 'primary': 0.30; 'problem.': 0.32; 'mac': 0.32; 'message.': 0.33; 'comments': 0.33; 'anyone': 0.33; 'to:addr :python-list': 0.33; 'skip:- 20': 0.34; 'program,': 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'created': 0.36; 'explain': 0.36; 'but': 0.36; 'test': 0.36; 'skip:p 20': 0.36; 'turn': 0.36; 'why': 0.37; 'rather': 0.37; 'received:209': 0.37; 'comment': 0.38; 'skip:o 20': 0.38; 'advice': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'below,': 0.60; 'respect': 0.63; 'success': 0.63; 'making': 0.64; 'saw': 0.75; 'altered)': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=/ohOylsf+KFdmEy4f9qgt+VB5xsX5MM2crmvmiv555Y=; b=0hKXf8fXkNLflDB/YTT9XtzhzsJTMBweNhgNJOYX5t56CYDSiH61PEwOW0770p5EEq t1+oWT2o7btfF6cpeUsb+knjt2hoVOVt08YUUlD3Z9Lkc3R0P430UUvfswBiyVT0xdMv iDZBe/VcgUPKer8ajDse4RzjHn9BzKhkLIFEYGYVzblxH1OhtcQ+ItvFafw0xGaVgdlU qSGDfXbTACTsKHRKTxJa+g/QKm7DUsHmpTwZQjFPHfSz35BCEHLWSoF8x8qhnlyo9KfW r4K7jFt6nCsZ5Ka1Rqhd9h/uj/kN0Rj/8ulNcWLD2tAShAarcMH3YpNsCkCNE3y0qg6f nN/A== MIME-Version: 1.0 Date: Wed, 18 Jul 2012 17:03:52 -0600 Subject: A thread import problem From: Bruce Sherwood To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342652635 news.xs4all.nl 6848 [2001:888:2000:d::a6]:34389 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25591 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. 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)