6

i want to execute a function in every 3 second the code works if i call a function without arguments like below:

def mytempfunc():
    print "this is timer!"
    threading.Timer(5, mytempfunc).start()

but if i call a function with argument like this:

def myotherfunc(a,b,c,d):
    print "this is timer!"
    threading.Timer(5, myotherfunc(a,b,c,d)).start()

the new thread will be created and started immediately without waiting for 5 seconds. is there anything that i missed?

3
  • The tabs on this aren't right and your second example is calling mytempfunc, which doesn't seem right. Could you edit your question? Commented May 24, 2013 at 19:11
  • Tabs still look off... Commented May 24, 2013 at 19:17
  • note: Timer() executes the function only once. See related question you want to call function repeatedly every n seconds Commented May 24, 2013 at 19:27

1 Answer 1

20

Try this:

threading.Timer(5, myotherfunc, [a,b,c,d]).start()

In your code, you actually call myotherfunc(a,b,c,d), rather than passing your function and arguments to the Timer class.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent catch. I am new to python and couldn't figure out why the code was behaving the way it was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.