[Python Tips] Apscheduler

in utopian-io •  3 months ago

Introducing Apscheduler

Apscheduler is a job manager (similar to cron) to run sections of code on a schedule. I use it frequently to manage jobs that need to run at a set interval.

Apscheduler can be used to schedule code execution in three different ways:

  • Cron style scheduling
  • Interval system scheduling
  • One-off execution

Job Persistence

If you want to use a database to store jobs, you can use any of these supported databases:

Apscheduler can be used to save jobs in a database to survive restarts and reboots. The job scheduler can persist as well as run jobs that should have run when it was offline.

  • Any supported SQLAlchemy DB
  • MongoDB
  • Redis
  • RethinkDB
  • ZooKeeper

Installing Apscheduler

I recommend using pip or better yet pipenv

pip install apscheduler

How to use Apscheduler

I am not going to cover all use cases as Apscheduler is a very powerful module. In most cases, I use the BackgroundScheduler but there are seven different schedulers depending on your needs. For example, there is a blocking scheduler if you want to force jobs to be executed in a single thread. There are customized schedulers for Qt, Twisted, Tornado, and Async.

Setup

You have import the correct scheduler you want to use.

from apscheduler.schedulers.background import BackgroundScheduler

You also have to instantiate the scheduler.

scheduler = BackgroundScheduler()

This setup will store the job queue in memory and will not persist across restarts and reboots. You can use a database if you need this functionality.

One of the great things about Apscheduler is that it allows you to easily multi-thread your application without dealing with threads directly.

Adding a job

Once you have the boilerplate finished, you need to add a job. You have two choices here, use add_job() or decorate a function with scheduled_job()

I am going to use add_job() as it is the most straightforward to use.

scheduler.add_job(load_blacklist, 'interval', minutes=10, id='load_blacklist')

In some of my scripts that don't yet support the Global Blacklist API, I load a local copy of my blacklist. I do this using an interval based job schedule every 10 minutes.

Starting the scheduler

Once you have added your jobs, you can start the scheduler.

scheduler.start()

You can also stop and pause the scheduler. You can and list, modify, and remove jobs.

Advanced Usage

There is a lot of advanced functionality in Apscheduler that allows for a ton of customization.

Examples
  • max_instances - By default, Apscheduler supports 10 threads but there is a limit of one instance of any specific job. This allows you to run multiple instances of a specific job at once.
  • misfire_grace_time - This is used to run jobs while the scheduler was unable to run a job because it was offline or busy.

Logging

Apscheduler supports the logging module and has its own logger named apscheduler.

Example logging boiler plate

import logging

logging.basicConfig()
logging.getLogger('apscheduler').setLevel(logging.INFO)

This should give you enough information to see the value of Apscheduler and how to start using it for your Python projects. More information can be found in the Online Documentation and you can find examples in the examples folder.

Apscheduler Github Repo

https://github.com/agronholm/apscheduler

My Python Tips Series

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for your contribution.

  • The functions you put in your tutorial are already well documented here.

  • Try to come up with new and more innovative/useful ways to utilize Apscheduler.

Please read the guidelines here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

·

It's a tutorial for people that don't even know it exists and why to use it, like most of my Python tutorial series.

I will not use Utopian tag next time, figured I'd add it since I see so many other tutorials there.

·
·

Yeap, they have tight regulation there. Great tutorial btw, I'm picking up Python recently as a hobby.

Posted using Partiko Android

·
·

hey @themarkymark!
It wasn't the moderator's intention to push you away from using Utopian tag, sorry if his note seemed as such!
It was simply a suggestion to provide some different approaches, additional insights, or applied use cases besides what is available in the standard documentation.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

·

Thank you for your review, @portugalcoin!

So far this week you've reviewed 20 contributions. Keep up the good work!

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Well @themarkymark not sure but that flown way over my head. I would love to learn programming code. What would be your suggestion on a coding language to learn? I made a post awhile back about wanting to but the limited comments I received was python!

·

Python or JavaScript are best place to start.

·
·

Thanks for your input!

Hi @themarkymark!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 7.927 which ranks you at #31 across all Steem accounts.
Your rank has improved 1 places in the last three days (old rank 32).

In our last Algorithmic Curation Round, consisting of 188 contributions, your post is ranked at #114.

Evaluation of your UA score:
  • Your follower network is great!
  • The readers like your work!
  • Your contribution has not gone unnoticed, keep up the good work!

Feel free to join our @steem-ua Discord server