1

Using the following to set a 30 second break every 60 seconds, however I do not want the break to start as soon as the script is first run. How can I add an initial delay to the first break?

def Breaktime():
  threading.Timer(60, Breaktime).start()
  print("Breaking for 30 seconds")
  time.sleep(30)

Breaktime()
2
  • Just add ‘time.sleep(30)’ before you call the function. Commented Jun 22, 2021 at 21:09
  • What do you mean by "30 second break"? What you have there starts a new thread and puts that thread to sleep, uselessly. It isn't going to affect the rest of your program. Is that what you were after? If you want the main code to pause, then you have to put the sleep in the main code. Commented Aug 23, 2021 at 18:19

1 Answer 1

1

How about passing an argument? Here is one way to do it.

def Breaktime(break_time=0):
  threading.Timer(60, Breaktime, kwargs={"break_time": 30}).start()
  print("Breaking for 30 seconds")
  time.sleep(break_time)

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

1 Comment

The rest of the script continues to run when I do this, it prints that it's breaking but doesn't actually stop.

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.