Managing Scheduled Functions in Your Python App

Olayinka Omole
tiltblog
Published in
2 min readJun 5, 2018
Photo by Agê Barros on Unsplash

Creating and managing Cron Jobs used to be one of the most dreaded tasks for me when I started coding. Mostly ‘cause I started out building simple apps in PHP on shared hosts. I couldn’t figure out how the timing for the jobs and how selecting the scripts to run worked.

In case you’re unfamiliar with Cron Jobs, here’s Wikipedia’s simple explanation:

People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.

It became much easier working with scheduled functions or scripts in JavaScript (Node.js) as you can easily define when you want certain functions to run using packages like node-schedule.

Yesterday, I had the need to run scheduled functions in my Flask app and discovered some awesome libraries for doing this.

APScheduler

This library works great when you want to run your scheduled functions in the background and it is the one I selected for my little app.

You can define a job to run in the background in your Flask app like this:

The above job simply prints the current time every 2 seconds.

APScheduler also has good documentation, you can check it out here.

Schedule

I also discovered Schedule, an in-process Python scheduler. I especially love the syntax for this library and how easy the creators made it to define jobs.

You can schedule a simple job with the library like this:

Our defined job here also prints the time every 2 seconds. I think Schedule will work great for scheduling in simple python scripts. I’m definitely itching to try it out soon. You can check out its docs here.

Conclusion

I’ve been quite happy working with APScheduler, even though I haven’t used it for any super complicated things yet. How do you manage scheduled functions in your apps? You can share in the comments section!

--

--

Olayinka Omole
tiltblog

JavaScript, Python and PHP Developer. I love to invent and learn about things.