[Tutor] timed functions? Timeouts?

W W srilyk at gmail.com
Tue May 20 05:23:17 CEST 2008


Those examples are really helpful and I'm pretty sure they'll do what
I need, I'll just have to play with them a little more.

I do have a question about one of the examples though!

Specifically this part:

  4 class Operation(threading._Timer):
  5     def __init__(self, *args, **kwargs):
  6         threading._Timer.__init__(self, *args, **kwargs)
  7         self.setDaemon(True)

So in trying to understand the class definition, I found this:

"In Python, the ancestor of a class is simply listed in parentheses
immediately after the class name."

So does that mean that Operation is similar or the same as say:

Operation = threading._Timer?

Then I'm a little confused by the * and ** - they look just like the
pointer and pointer to a pointer in C++, but do they perform the same
function in python?

TIA,
Wayne

On Mon, May 19, 2008 at 8:23 PM, Kent Johnson <kent37 at tds.net> wrote:
> On Mon, May 19, 2008 at 5:28 PM, W W <srilyk at gmail.com> wrote:
>> Now, when I look at that example and try to translate the timeout to
>> what I think is going on "behind the scenes" I'm pretty sure it spawns
>> a thread that does something similar to this:
>>
>>   1 import time
>>  2
>>  3 def timer(end, exe):
>>  4     start = time.time()
>>  5     while True:
>>  6       now = time.time()
>>  7       if (now - start) >= end:
>>  8         break
>>  9     exe()
>>  10
>>  11 def execute_this():
>>  12    for x in range(1, 10):
>>  13      print x
>>  14
>>  15 timer(5, execute_this)
>>
>> Am I correct? Is there a python built-in for this? Where should I go
>> to learn more? Is there anything "wrong" with using a function like
>> this?
>
> You are pretty much on the right track but you should put a
> time.sleep() in your loop or it will chew up all available CPU time.
> Even better, figure out how long you need to sleep.
>
> The sched module in the standard library might help though it doesn't
> seem to do any threading. Also look at these recipes:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496800
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/114644
>
> Kent
>



-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi


More information about the Tutor mailing list