Re: A thread import problem

Discussion in 'Python' started by Dennis Lee Bieber, Jul 19, 2012.

  1. 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
    <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
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Jul 19, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Seefeld
    Replies:
    3
    Views:
    958
  2. per9000
    Replies:
    7
    Views:
    403
    Magnus Lycka
    Feb 27, 2006
  3. Bruce Sherwood

    A thread import problem

    Bruce Sherwood, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    155
    Bruce Sherwood
    Jul 19, 2012
  4. Dieter Maurer

    Re: A thread import problem

    Dieter Maurer, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    141
    Dieter Maurer
    Jul 19, 2012
  5. Bruce Sherwood

    Re: A thread import problem

    Bruce Sherwood, Jul 20, 2012, in forum: Python
    Replies:
    0
    Views:
    152
    Bruce Sherwood
    Jul 20, 2012
Loading...

Share This Page