how to run a function in interval in python

# this makes program sleep in intervals
from time import time, sleep
while True:
    sleep(1 - time() % 1) # run every 1 second... you can change that
	# thing to run

4
2
Veegeecee 110 points

                                    # This runs test() function in intervals of 1 second
from threading import Timer
run = True
def test():
	global run
	print("something")
	if run:
		Timer(1, test).start()

test()
# now whenever you set run to False the test function won't run anymore
# and of course if you dont set it to False it will run forever

4 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
on interval python What is an interval python python run on time interval how to set a interval in oython interval for run a function in python how to call python function interval t.intervalt.interval python python in interval how to set a interval python python interval example python run function interval execute function python interval python set interval how to write interval in python python create interval call function at some interval python set interval python how to run python script in time interval Interval in python pyhton run interval function how to run a python function in a time interval how to create an interval in python interval python set interval in python interval python python defining an interval python interval in interval python How to run function in interval python run function at specific interval python python run function every n seconds start function after x seconds python how to run some code with a interval of time in python call the function in intervals python run thread every 5 seconds python python timer event api call every 5 sec python timer event python python loop every 5 seconds why is my python code just show for few secounds python call function every 5 seconds run code every 10 seconds python run code at interval python call function every 5 seconds python how to call python function at interval python interval timer how to run a function continuously in python python do action every second python run code every 5 minutes how to repeat code every 5 seconds in python run code every 0.01 seconds python python run code every second python do every 5 seconds how to run a python function in specific interval python print ok each 2 minutes how to repeat something in python ever second run a function at a certain interval python repeat a function after certain interval python make function inteval python python run code every n seconds how to call a function every 5 seconds in python python timer events how to execute something every x seconds thread in python do something every second python how to run a function every 5 seconds in python how to set interval in python how to run a function in interval in python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source