Re: A thread import problem

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

  1. On Sat, 21 Jul 2012 10:11:30 -0600, Bruce Sherwood
    <> declaimed the following in
    gmane.comp.python.general:


    >
    > ---------------------------
    > testABA.py -- execute this file
    >
    > from ABA import *
    > print('exec testABA')
    > from math import sin
    > print(sin(3.14159/6))
    >
    > ----------------------------
    > testABA_noimport.py -- a version of testABA.py without the import of ABA
    >
    > print('exec testABA_noimport')
    > from math import sin
    > print(sin(3.14159/6))
    >
    > -----------------------------
    > ABA.py
    >
    > from thread import start_new_thread
    > from time import sleep
    > import sys
    >
    > user = 'testABA_noimport'
    > start_new_thread(lambda: __import__(user), ())
    > print('after start_new_thread\n')
    >
    > while True:
    > sleep(1)
    >


    And all along you are still coding where imported modules are doing
    work DURING THE IMPORT. Anything beyond initializing module level
    constants or importing modules needed by the module should be placed
    into a function which can be executed by the top-level importer AFTER
    the import has finished.

    -=-=-=-=- testABA.py
    print ("testABA: top")

    from math import sin, pi
    import time

    print ("testABA: defining worker function")
    def runner(): #this is the key -- a function IN the module
    #that does the real work
    print ("testABA.runner: starting work\n")
    time.sleep(2.5)
    print (sin(pi))
    time.sleep(2.5)
    print ("\ntestABA.runner: end of work")

    print ("testABA: after set-up")

    if __name__ == "__main__":
    print ("testABA: was not an import, running main task")
    runner() #invoke the function if module is not imported
    print ("testABA: end")
    -=-=-=-=-

    -=-=-=-=- ABA.py
    import threading

    USER = "testABA"

    print ("ABA: importing " + USER)
    modl = __import__(USER)

    print ("ABA: starting runner")
    th = threading.Thread(target=modl.runner)
    th.start()

    print ("ABA: waiting for completion")
    th.join()

    print ("ABA: done")
    -=-=-=-=-

    And showing the results...

    -=-=-=-=-
    E:\UserData\Wulfraed\My Documents\Python Progs>testABA
    testABA: top
    testABA: defining worker function
    testABA: after set-up
    testABA: was not an import, running main task
    testABA.runner: starting work

    1.22460635382e-016

    testABA.runner: end of work
    testABA: end

    E:\UserData\Wulfraed\My Documents\Python Progs>ABA
    ABA: importing testABA
    testABA: top
    testABA: defining worker function
    testABA: after set-up
    ABA: starting runner
    testABA.runner: starting work

    ABA: waiting for completion
    1.22460635382e-016

    testABA.runner: end of work
    ABA: done
    -=-=-=-=-

    Note that "testABA.py" is designed to be used as a stand-alone
    program, but is also designed to be imported where all the real work is
    done from a function that is NOT run during the import. The program that
    imports "testABA" is then responsible for actually starting the worker.

    I put the sleeps into testABA.runner() so that you can see that the
    main process isn't blocked (note the "ABA: waiting..." output)


    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Jul 21, 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:
    949
  2. per9000
    Replies:
    7
    Views:
    398
    Magnus Lycka
    Feb 27, 2006
  3. Bruce Sherwood

    A thread import problem

    Bruce Sherwood, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    149
    Bruce Sherwood
    Jul 19, 2012
  4. Dennis Lee Bieber

    Re: A thread import problem

    Dennis Lee Bieber, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    164
    Dennis Lee Bieber
    Jul 19, 2012
  5. Dieter Maurer

    Re: A thread import problem

    Dieter Maurer, Jul 19, 2012, in forum: Python
    Replies:
    0
    Views:
    138
    Dieter Maurer
    Jul 19, 2012
Loading...

Share This Page