Task
Overview¶
This note will introduce ways to execute scheduled tasks in Python. Mainly include four ways: function sleep
, class Timer
, module sched
and frame APScheduler
.
Loop and sleep¶
import time
# Run every other n seconds.
def task(n):
while True:
print('Hello World!')
time.sleep(n)
task(5)
Notes: The program will be BLOCKED in the time.
Timer¶
It's in the module threading
, passing the interval of time (unit: seconds), the function to execute and arguments of the function as three arguments.
from threading import Timer
def f(s):
print(s)
# Execute only once after 5 seconds.
t = Timer(5, f, ('Hello World!',))
t.start()
# Execute the task and then define next one.
def task(s, interval):
print(s)
Timer(interval, task, (s, interval)).start()
Notes:
- The program won't be blocked in this way.
Timer
won't loop. It's over after executed.
sched¶
import sched
import time
# initialize scheduler
# pass the time and delay functions
scheduler = sched.scheduler(time.time, time.sleep)
def task(s, interval):
print(s)
# pass the interval, priority, function to execute and arguments of the function
scheduler.enter(interval, 0, task, (s, interval))
# the first one
scheduler.enter(0, 0, task, (5, 'Hello World!'))
scheduler.run()
Notes: Same as Timer
, sched
won't loop either.
APScheduler¶
Install with pip install APScheduler
.
Four Components¶
Trigger defines which task to run next. There are three built-in triggers:
- date: specified time
- interval: fixed time intervals
- cron: cron expression
Job Store, as the name suggests, stores jobs to be scheduled. MemoryJobStore
is used by default. Database is also available if necessary.
Executor execute jobs in the thread or thread pool. It will notify Scheduler
when jobs are finished. Two common ones are ProcessPoolExecutor
and ThreadPoolExecutor
.
Schedulers configures the above components, such as adding a job. As follow are all the schedulers:
- BlockingScheduler: use when the scheduler is the only thing running in your process
- BackgroundScheduler: use when you’re not using any of the frameworks below, and want the scheduler to run in the background inside your application
- AsyncIOScheduler: use if your application uses the asyncio module
- GeventScheduler: use if your application uses gevent
- TornadoScheduler: use if you’re building a Tornado application
- TwistedScheduler: use if you’re building a Twisted application
- QtScheduler: use if you’re building a Qt application
Configuration¶
To be continued...