How do you schedule tasks in python? (AI) Flashcards

(5 cards)

1
Q

What is the simplest, most intuitive Python library for running recurring tasks within a running Python script using a human-readable, English-like syntax?

A

The schedule library. It requires a while True loop and time.sleep() to continuously check and run pending jobs.

import schedule
import time

Task definition
def my_task():
    print("Task is running...")

Scheduling
schedule.every(10).minutes.do(my_task)
while True:
    schedule.run_pending()
    time.sleep(1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which Python library offers a more powerful and flexible approach to scheduling, supporting cron-like expressions, specific dates, and interval-based scheduling?

A

APScheduler (Advanced Python Scheduler). It’s suitable for more complex scheduling needs and battle-tested in production environments.

from apscheduler.schedulers.blocking import BlockingScheduler

Task definition
def my_task():
    print("Task is running...")

Scheduling
scheduler = BlockingScheduler()
scheduler.add_job(my_task, 'interval', seconds=10)
scheduler.start()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you schedule a Python script at the operating system level on Linux/Unix systems, allowing it to run even if your Python interpreter isn’t active?

A

By using cron jobs. You define the schedule in a crontab file using cron syntax, which then executes the Python script directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Windows equivalent of a cron job for scheduling Python scripts to run automatically on a specific schedule?

A

The Windows Task Scheduler. You can configure a task to run the Python executable (python.exe) and pass your script’s file path as an argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a python library that interfaces with linux crontab?

A
python-crontab

Code example:

import time
from crontab import CronTab

Task definition
def my_task():
  print("Task is running...")

Scheduling
cron = CronTab(user='your_username')
job = cron.new(command='python path_to_your_script.py')
job.minute.every(10)
cron.write()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly