Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25773
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'importing': 0.04; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'executed': 0.07; 'finished.': 0.07; 'blocked': 0.09; 'imported': 0.09; 'imports': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'thread': 0.11; 'sat,': 0.15; '#this': 0.16; '(note': 0.16; 'constants': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'set-up': 0.16; 'subject:import': 0.16; 'threading': 0.16; 'top-level': 0.16; 'true:': 0.16; 'module': 0.19; 'math': 0.20; 'import': 0.21; 'subject:problem': 0.22; 'bruce': 0.23; 'task': 0.23; 'coding': 0.27; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'sleep': 0.29; 'skip:_ 10': 0.29; "skip:' 10": 0.30; 'function': 0.30; 'file': 0.32; 'running': 0.32; 'print': 0.32; 'defining': 0.33; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'skip:- 20': 0.34; 'program,': 0.34; 'done': 0.34; 'along': 0.35; 'needed': 0.35; 'doing': 0.35; 'received:org': 0.36; 'but': 0.36; 'modules': 0.36; 'anything': 0.36; 'should': 0.36; 'charset:us- ascii': 0.36; 'skip:p 20': 0.36; 'beyond': 0.37; 'execute': 0.37; 'does': 0.37; 'level': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'real': 0.61; 'jul': 0.65; 'completion': 0.78; 'user)': 0.84; 'dennis': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
| Subject | Re: A thread import problem |
| Date | Sat, 21 Jul 2012 18:16:00 -0400 |
| Organization | > Bestiaria Support Staff < |
| References | <CA+WuaScBDU_5LcrCrTtDr6N3gHCk9hQ03fA=DT359GCRY=a=KQ@mail.gmail.com> <CA+WuaSeRExxaD9yCNWhXoWqsV_ZqP8DOvdErMRdNS-H_UtzQyg@mail.gmail.com> <87a9yt7bw6.fsf@handshake.de> <CA+WuaSc0Rm5s0CXsm7q7-gfyw+jQxpAGkuajpgFyoAHnTP12sQ@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | adsl-76-249-19-203.dsl.klmzmi.sbcglobal.net |
| X-Newsreader | Forte Agent 3.3/32.846 |
| X-No-Archive | YES |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2402.1342908961.4697.python-list@python.org> (permalink) |
| Lines | 126 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1342908961 news.xs4all.nl 6869 [2001:888:2000:d::a6]:44284 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:25773 |
Show key headers only | View raw
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 comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: A thread import problem Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-07-21 18:16 -0400
csiph-web