Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25773 > unrolled thread
| Started by | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| First post | 2012-07-21 18:16 -0400 |
| Last post | 2012-07-21 18:16 -0400 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: A thread import problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-07-21 18:16 -0400
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2012-07-21 18:16 -0400 |
| Subject | Re: A thread import problem |
| Message-ID | <mailman.2402.1342908961.4697.python-list@python.org> |
On Sat, 21 Jul 2012 10:11:30 -0600, Bruce Sherwood
<bruce.sherwood@gmail.com> 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
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to top | Article view | comp.lang.python
csiph-web