Code introduction
This function uses the Schedule library to schedule a task that prints a random greeting every 10 seconds. It defines a function `random_task` that uses the `every` method from the Schedule library to run the `print_random_greeting` function every 10 seconds. The `print_random_greeting` function randomly selects a greeting from a predefined list and prints it. Finally, an infinite loop keeps the program running and periodically checks if it needs to run the scheduled tasks.
Technology Stack : Schedule library, scheduled tasks
Code Type : Timed task
Code Difficulty : Intermediate
import random
import schedule
import time
def random_task():
# This function schedules a random task every 10 seconds
schedule.every(10).seconds.do(print_random_greeting)
def print_random_greeting():
greetings = ["Good morning", "Hello", "Hi", "Welcome", "Good afternoon"]
print(random.choice(greetings))
# Schedule the random task to run every 10 seconds
random_task()
# Keep the program running
while True:
schedule.run_pending()
time.sleep(1)