1

How to execute a function in Python based on time provided by seconds argument.

def timerfn(seconds, fn, *args):
    #start some timer
    #if function first launch is > "seconds", abort
    #if function doing great.. repeat it and stop at "seconds"

As i'm thinking over it time.clock() can't really help here, because a function can be really tough and it will not ever reach a counter-timer to show some result or abort.

Do i have to use threading module? i don't know much about threads, but from what i read - you can have only a one thread in work according to GIL, and this process really needs 2 threads at same time.. or some other trick that i don't know.

Umren
  • 392
  • 4
  • 12

1 Answers1

0

The GIL means, always just one Thread is running, but it doesn't mean that you're restricted to one Thread.

Sounds like you want threading.Timer.

dav1d
  • 5,917
  • 1
  • 33
  • 52
  • how can threading.Timer help here? can it dynamically break out of infinite loop or something like that? – Umren Jun 17 '12 at 13:42
  • Yes if you use it correctly you can use it to set an event (also in the threading module) which breaks out of the loop – dav1d Jun 17 '12 at 13:43