[Tutor] Repeat function until...

Emile van Sebille emile at fenx.com
Wed Jun 23 16:04:42 CEST 2010


On 6/23/2010 6:51 AM Steven D'Aprano said...
> # untested
> def call_again(n, func, *args):
>      """call func(*args) every n seconds until ctrl-D"""
>      import time
>      try:
>          while 1:
>              start = time.time()
>              func(*args)
>              time.sleep(n - (time.time()-start))

Watch out for this -- you may want to do

   time.sleep(n - max(0,(time.time()-start)))

to avoid passing sleep a negative number which causes the big sleep...

Emile



More information about the Tutor mailing list