Scheduling Basic Tasks
Implement simple scheduling mechanisms to run your bots at specific intervals for continuous automation.
Why Schedule Your Bot?
Ever wished your bot could run itself without you pressing "Go"? That's where scheduling comes in!
Scheduling allows your bot to perform tasks automatically at specific times or intervals. This is super useful for:
- Monitoring: Checking a website every hour for updates.
- Data Collection: Scraping news articles daily.
- Notifications: Sending alerts when a price changes.
Let's learn how to make your bot work on its own schedule.
Pausing Your Bot with `time.sleep()`
The simplest way to introduce a delay in your Python bot is using the time.sleep() function. It pauses your program for a specified number of seconds.
This is great for waiting between requests to avoid overwhelming a server, or just to slow things down.
Try this simple example:
import time
def main():
print("Starting task...")
time.sleep(2) # Pause for 2 seconds
print("Task finished after delay.")
if __name__ == "__main__":
main()