Web Spider

Peter Hansen peter at engcorp.com
Wed Jul 7 15:29:41 CEST 2004


Thomas Lindgaard wrote:
> On Tue, 06 Jul 2004 11:19:01 -0400, Peter Hansen wrote:
>>Answered indirectly in this FAQ:
>>http://www.python.org/doc/faq/programming.html#how-do-i-find-the-current-module-name
> 
> Let me just see if I understood this correctly...
> 
> The reason for using the construct is to have to "modes" for the script:
> One for running the script by itself (ie. run main()) and one for when it
> is included from somewhere else (ie. main() should not be run unless
> called from the surrounding code).

Yep.
> 
> Can a thread die spontaneously if for instance an exception is thrown?

The interactive prompt is your friend for such questions in Python.
Good to get in the habit of being able to check such stuff out
easily:

c:\>python
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import time, threading
 >>> class Test(threading.Thread):
...   def run(self):
...     while 1:
...       time.sleep(5)
...       1/0
...
 >>> a = Test()
 >>> threading.enumerate()
[<_MainThread(MainThread, started)>]
 >>> a.start()
 >>> threading.enumerate()
[<Test(Thread-2, started)>, <_MainThread(MainThread, started)>]

 >>> # wait a few seconds here
Exception in thread Thread-2:
Traceback (most recent call last):
   File "c:\a\python23\lib\threading.py", line 436, in __bootstrap
     self.run()
   File "<stdin>", line 5, in run
ZeroDivisionError: integer division or modulo by zero

 >>> threading.enumerate()
[<_MainThread(MainThread, started)>]

Tada!  The answer is yes. :-)

-Peter



More information about the Python-list mailing list