Threads and import

rsoh.woodhouse at googlemail.com rsoh.woodhouse at googlemail.com
Wed May 28 17:19:40 EDT 2008


On May 28, 8:52 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> rsoh.woodho... at googlemail.com schrieb:
>
>
>
> > On May 28, 8:26 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
> >> rsoh.woodho... at googlemail.com schrieb:
>
> >>> Hi,
> >>> I'm trying to work out some strange (to me) behaviour that I see when
> >>> running a python script in two different ways (I've inherited some
> >>> code that needs to be maintained and integrated with another lump of
> >>> code). The sample script is:
> >>> # Sample script, simply create a new thread and run a
> >>> # regular expression match in it.
> >>> import re
> >>> import threading
> >>> class TestThread(threading.Thread):
> >>>     def run(self):
> >>>         print('start')
> >>>         try:
> >>>             re.search('mmm', 'mmmm')
> >>>         except Exception, e:
> >>>             print e
> >>>         print('finish')
> >>> tmpThread = TestThread()
> >>> tmpThread.start()
> >>> tmpThread.join()
> >>> import time
> >>> for i in range(10):
> >>>     time.sleep(0.5)
> >>>     print i
> >>> # end of sample script
> >>> Now if I run this using:
> >>> $ python ThreadTest.py
> >>> then it behaves as expected, ie an output like:
> >>> start
> >>> finish
> >>> 0
> >>> 1
> >>> 2
> >>> ...
> >>> But if I run it as follows (how the inherited code was started):
> >>> $ python -c "import TestThread"
> >>> then I just get:
> >>> start
> >>> I know how to get around the problem but could someone with more
> >>> knowledge of how python works explain why this is the case?
> >> Works for me. And I don't see any reason why it shouldn't for you -
> >> unless you didn't show us the actual code.
>
> >> Diez
>
> > Strange. That is the code exactly as I run it using python 2.4.4 2.5.1
> > on Ubuntu 7.10. Which version of python/what platform were you using?
>
> mac-dir:/tmp deets$ python
> Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
> [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> Welcome to rlcompleter2 0.96
> for nice experiences hit <tab> multiple times
>  >>>
>
> But I doubt this changes anything.
>
> Diez

Hmm. Just tested it again on OS X Python 2.4.4 and custom build of
Python 2.4.5 on Debian and get the same results as I had before.

Thanks,
Rowan



More information about the Python-list mailing list