I want to be able to be able to run a function at a random time as long as the program is running.
Let's say I have this function:
def sample_func:
print("Function is running.")
and some other code running. As long as the program is being run, I want this function to run at a random amount of time between 5 to 10 minutes while other code is being run. Is this possible?
Here's the code you're looking. Set the sleep timer for 5-10 minutes. Insert your main flow code inside the "main flow" function.
import random
import threading
import time
def random_function():
print("Function is running.")
def call_random_function():
while True:
time.sleep(random.randint(1, 5))
random_function()
def main_flow():
"""
Do your main flow here
"""
a = 1
while a < 10:
print(a)
a += 1
time.sleep(10)
if __name__ == '__main__':
threading.Thread(target=call_random_function).start()
main_flow()
print("Program is finished.")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.