Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: A thread import problem

 
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      07-21-2012
On Sat, 21 Jul 2012 10:11:30 -0600, Bruce Sherwood
<(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
 
 
Reply

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 Dieter Maurer Python 0 07-19-2012 08:31 AM
Re: A thread import problem Dennis Lee Bieber Python 0 07-19-2012 12:47 AM
A thread import problem Bruce Sherwood Python 0 07-18-2012 11:03 PM
problem(s) with import from parent dir: "from ../brave.py 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



Advertisments