Running tasks on a schedule is not a rare use case. Pretty much every programmer does it.
The standard technique used for scheduling is using Cron jobs. I have no objection to using Cron and still think it’s a stable way.
But what if you want a scheduler all in Python? What if you also want it easier to configure?
That’s the focus of this post.
Scheduling tasks in Python
Task scheduling in Python is made easy with the Python package called ‘schedule.’ You can install it from the PyPI repository.
The schedule uses a familiar builder pattern. It lets you construct schedules almost as you’d speak in a natural language. That is, if you want to run a function (say
send_email) once every hour, you could do it like:
That’s very close to a normal conversation, isn’t it? Here’s a complete script to send emails in a schedule, but once a day instead of every hour.
The above code will call the send_email function at 2.45 PM per your system time.
Besides the ease of scheduling, the package offers a range of other features.
Scheduling with a decorator
I have a personal preference for using decorators whenever possible. It’s cleaner and more elegant.
The ‘schedule’ package supports decorators out of the box. Here’s our example of sending emails, this time using decorators.
In the example above, you can also see that we’ve done two scheduled for the same task. The first one will call the function every 10 seconds. The second one will call it every 5 seconds.
Run scheduled tasks with parameters.
Scheduling alone is not very useful. We often want more control over the execution of the function. We do it by passing arguments.
As we see, our send email function takes an optional argument, email. When scheduling, we can pass in the parameters as keyword arguments.
These are just a few examples of using the library in Python. Please refer to the official documentation for more valuable examples.
Scheduling tasks with the cron tab— the conventional way.
Crontab is the most popular technique for running scheduled tasks. It’s famous for a couple of reasons.
One it’s provided by the OS. Crontab is for Linux. Even Windows users can use Crontab using WSL.
The other reason is that Crontab is independent of the tasks’ programming language. You can also use the same technique to run a node js script.
First, we need to modify our code to run on demand instead of on a schedule because Crontab will take care of the scheduling.
We can run the above script in our terminal as follows:
We can start editing the Crontab with the following command.
The command prompt will ask you to choose an editor upon your first edit. I’ve chosen vim as it’s comfortable for me. But you can choose anything.
I’ve added the following line at the end of the file.
This configuration tells Crontab to run the
send_email.py script at 2.45 PM every day. Save it and close the file. Your script should run as expected.
If you’re not clear about the Crontab configurations, my best advice is to use Crontab.guru.
Crontab or Python Schedule?
Both tools are excellent for scheduling tasks. But depending on the circumstances, one works better than the other.
The plus point of Python’s schedule is its flexibility and friendliness. Anyone can understand the schedule just by reading it. You don’t even have to refer to the documentation.
Although crontab configurations are not that hard to understand, beginners will need a little effort to understand them.
Everything is in one place, which makes the schedule library straightforward. In Crontab, you’d have to edit the configuration file outside your developing module. If someone else wants to use your application, you’d have to instruct them.
Also, if I want to create a new schedule, All I have to do is to annotate the function with another.
@repeat. Also, in Crontab, you’d add another configuration line. But not as swift as you’d do in a Python script.
These pros make Python’s schedule library suitable for most use cases. But take a look at the library’s documentation page.
This library is designed to be a simple solution for simple scheduling problems. You should probably look somewhere else if you need to remember schedule between restarts, sub-second precision execution, multiple threads, time zones, workdays or holidays support. — schedule library docs.
Running tasks on a schedule is a frequent need in software development. I often find it useful to run the little productive hacks I do on the go.
I’ve been using Crontab for several years, thinking that it’s the only way to do the job. But the schedule Python library made it super simple.
It may not be the perfect tool for every instance. But not everything we do daily needs more sophisticated techniques.
Now, I’ve converted many such periodic executions to Python schedules.
Not a Medium member yet? Please use this link to become a member because I earn a commission for referring at no extra cost for you.