You can download this code by clicking the button below.
This code is now available for download.
This function uses the APScheduler library to schedule the generation of 10 random words every hour, which are then printed.
Technology Stack : APScheduler, string, random, datetime
Code Type : Generate random words on a regular basis
Code Difficulty : Intermediate
def random_word_generator(num_words=10):
import random
import string
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime, timedelta
def generate_word():
return ''.join(random.choices(string.ascii_letters + string.digits, k=10))
def print_random_words():
words = [generate_word() for _ in range(num_words)]
print("Generated words:", words)
scheduler = BackgroundScheduler()
scheduler.add_job(func=print_random_words, trigger="interval", hours=1)
scheduler.start()
try:
# To keep the main thread alive
while True:
time.sleep(2)
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()