Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: A thread import problem

Thread Tools

Re: A thread import problem

Dennis Lee Bieber
Posts: n/a
On Wed, 18 Jul 2012 17:03:52 -0600, Bruce Sherwood
<(E-Mail Removed)> declaimed the following in

> 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
<name> local: yes, retrieve local value; no, is <name> 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


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

if __name__ == "__main__":
wrkr = threading.Thread(target=theWork, args=(), kwargs={})

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 <G>
class Something(object):
def something():

are okay...

if sys.argv[1] == "debug":

should be inside a "def worker()" so that the code only runs when the
worker thread is started.

Secondly: read the first bullet under
(Your import is spawning a thread which then tries to perform an import)

Wulfraed Dennis Lee Bieber AF6VN Removed) HTTP://

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: A thread import problem Bruce Sherwood Python 0 07-20-2012 06:44 PM
Re: A thread import problem Dieter Maurer Python 0 07-19-2012 08:31 AM
A thread import problem Bruce Sherwood Python 0 07-18-2012 11:03 PM
problem(s) with import from parent dir: "from ../ import sir_robin" per9000 Python 7 02-27-2006 06:36 PM
Problem with import "from omniORB import CORBA, PortableServer" Stefan Seefeld Python 3 04-11-2005 08:54 PM