timer
superpollo
user at example.net
Tue Jun 30 09:04:11 EDT 2009
Paul Moore wrote:
> 2009/6/29 MRAB <python at mrabarnett.plus.com>:
>
>>superpollo wrote:
>>
>>>hi folks.
>>>
>>>the follwing shoud print 'stuff' for 3 seconds and then stop. why it does
>>>not work? (prints stuff forever)
>>>
>>> 1 #!/usr/bin/python
>>> 2
>>> 3 import threading
>>> 4 import sys
>>> 5
>>> 6 t = threading.Timer(3.0, sys.exit)
>>> 7 t.start()
>>> 8 while True:
>>> 9 print "stuff ",
>>>
>>
>>The Timer runs the function in another thread. Perhaps sys.exit is just
>>exiting that thread and not the main thread.
>
>
> sys.exit raises a SystemExit exception, which will get handled in the
> new thread (where it won't do anything). Conceded, this isn't
> particularly intuitive.
>
> For a non-toy example, you'd probably create an Event object, use your
> timer to set the event, and your while loop would do while
> event.is_set(), so the problem wouldn't arise.
>
> Paul.
so why this does not work?
1 #!/usr/bin/python
2
3 import threading
4
5 e = threading.Event()
6 t = threading.Timer(3.0, e.set())
7 t.start()
8 while not e.isSet():
9 print "stuff ",
it does *NOT* print (but it should, shouldn't it?), then exits after 3
sec but with error:
Exception in thread Thread-1:Traceback (most recent call last):
File "/usr/lib/python2.3/threading.py", line 442, in __bootstrap
self.run()
File "/usr/lib/python2.3/threading.py", line 575, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
what gives?
More information about the Python-list
mailing list