luana nastasi
luana nastasi

Reputation: 29

Python schedule a function

I have a problem. I would like to schedule a function at 09:00 for every fifteen minutes. and finish the execution at 11:00.

def function1():
     print("Hello")

Do you have any ideas?

Upvotes: 0

Views: 877

Answers (6)

Igor
Igor

Reputation: 61

Additionally to what mentioned above (cron or pure Python) you could try schedule module:

from datetime import datetime

import schedule


def function1():
    print("Hello")


schedule.every(15).minutes.do(function1)

while True:
    if datetime.now().hour == 9:
        schedule.run_pending()
    if datetime.now().hour == 11:
        schedule.clear()
        break

Upvotes: 0

Manu Valdés
Manu Valdés

Reputation: 2372

If you want to do this in pure Python with no external libraries or tools (like cron), you can do something like this, where you would calculate the time until your action, and sleep for that amount of time.

If you run this, the process will be running forever, sleeping until 9:00 every day, at which point it will run function1() and wait for the next quarter, until 11:00.

Beware that if function1() takes more than 15 minutes to run, this will skip that quarter execution and wait for the next one. Also if you start if after 9:00, it will wait for the next day before the first execution.

from datetime import datetime,timedelta

def wait_for_nine():
    now = datetime.now()
    if now.hour < 9:
        wait_for = datetime.replace( now, hour=9, minute=0, second=0)
    else:
        wait_for = datetime.replace( now + timedelta(days=1), hour=9, minute=0, second=0)
    delta = wait_for - datetime.now()
    time.sleep(delta.seconds)

def wait_fifteen():
    now = datetime.now()
    next_quarter = 15 * (int(now.minutes / 15) + 1)
    wait_for = datetime.replace(now, minute=next_quarter % 60, second=0) + timedelta(hours= int(next_quarter / 60))
    delta = wait_for - dt.datetime.now()
    time.sleep(delta.seconds)

while True:
    wait_for_nine()
    now = datetime.now()
    eleven = datetime.replace(now, hour=11,minute=0, second= 0 )
    while now <= eleven:
        function1()
        wait_fifteen()

Upvotes: 1

lesthaeghet
lesthaeghet

Reputation: 116

There are two possible approaches:

1) You can use your operating system's scheduling system to have your script ran at the appropriate times. On Windows, this is called the Task Scheduler. On Linux, this is called cron. See Henry Woody's reply for specifics on using cron.

2) You can write code to calculate exactly how long you need to wait until your code runs. Then, you run your code, and repeat. See this post for a lot more detail: Python - Start a Function at Given Time

The first option will be the easiest and seems like the most appropriate given your description, but both approaches have appropriate use cases.

Upvotes: 1

MJK
MJK

Reputation: 1401

You can use simple cron job to schedule your job or Try orchestration framework like Airflow - http://airflow.incubator.apache.org/

Airflow is a platform to programmatically author, schedule and monitor workflows.

Upvotes: 1

Henry Woody
Henry Woody

Reputation: 15662

If you're using a Unix machine, you can write a cronjob to run a Python file every 15 minutes (or on any schedule you like). The file should contain and call whatever functions you want.

Open your crontab with crontab or crontab -e in shell.

Then you can add, for example:

*/15 09-10 * * * python3 /path/to/my/file.py

Which will run every 15 minutes starting at 9:00 and the last run will occur at 10:45. If you want a run at 11:00 as the final run for the day, that will have to be another line in the crontab like:

0 11 * * * python3 /path/to/my/file.py

Upvotes: 1

BioGeek
BioGeek

Reputation: 22827

You don't mention your operation system.

On Linux and MacOS, use cron.

On Windows, use Scheduled Tasks.

Upvotes: 1

Related Questions