Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25596
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: A thread import problem |
| Date | 2012-07-18 20:47 -0400 |
| Organization | > Bestiaria Support Staff < |
| References | <CA+WuaScBDU_5LcrCrTtDr6N3gHCk9hQ03fA=DT359GCRY=a=KQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2282.1342658854.4697.python-list@python.org> (permalink) |
On Wed, 18 Jul 2012 17:03:52 -0600, Bruce Sherwood
<bruce.sherwood@gmail.com> 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
<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
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 <G>
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/
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: A thread import problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-07-18 20:47 -0400
csiph-web