this is a timer inside a game I programmed:
def function(event):
time.sleep(.2)
tx2 = time.time()
if tx2-tx1 > 0.7:
#do the repetitive stuff here
return function(1)
tx1 = time.time()
thread.start_new_thread(function,(1,))
is there a better way to write this?
to me it seems a bit dirty calling a recursive function and a new thread…
moreover it crashes after a while…
Your current example runs into the issue of recursion limits, because of the way it calls itself recursively. The stack size continues to grow and grow until it hits the default 1000, most likely. See this modified example:
Its probably easiest to use a custom Thread class that can run until you tell it to stop. This way the stack size doesn’t keep growing. It just loops and calls your handler function.
Here is a working complete example: