-1

I have a function and i need to run it after 3 days using python

def hello()
print("hello world")

The script will be running,How to print it every 3 days in python

user3254437
  • 75
  • 1
  • 2
  • 10
  • 1
    You can use cron jobs for that: https://stackoverflow.com/questions/11774925/how-to-run-a-python-file-using-cron-jobs – Adelina Aug 06 '18 at 11:58

2 Answers2

5

As @Nuts said in a comment, cron is the best option if you want to run an entire program every three days. However, if you're running a microservice or something and just want one particular method to execute every three days, you can use timers.

import threading

my_timer = None

def make_thread():
    # create timer to rerun this method in 3 days (in seconds)
    global my_timer
    my_timer = threading.Timer(259200, make_thread)
    # call hello function
    hello()

Just call make_thread() once to execute hello() the first time, and then it will call itself every three days (with a few seconds of margin for error, most likely), so long as the program remains running.

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • Thanks for your answer.I have python script with multiple functions running on a server everyday at 11am.I did what you have written and called my function inside make_thread().Will the specific function run after 3 days only? – user3254437 Aug 07 '18 at 09:59
2

If you have to use python, one possible way (adapted slightly from this thread which has other good information) is to use a massive sleep statement.

#!/usr/bin/python3

import time 

def hello():
    print("Hello, World")

while True:
    hello()
    time.sleep(3*24*60*60) 

Here is a more complete code snippet. I tested it for shorter intervals (not 3 days) but it should probably work

import numpy as np 
import time
import datetime

def function_to_call_periodically():
    print("Hello World")

# specify the interval and the time (3 days and at 11h00)
# The code below assumes the interval is more than a day
desired_time_delta_in_seconds = 3*24*60*60  # 3 days
desired_time_minutes = 00
desired_time_hours = 11

# set the starting time the code will run every 3 days from this date (at the specified time)
start_date = datetime.datetime(2018, 8, 7) # year, month date (hours, minutes, seconds will all be zero)
start_stamp = start_date.timestamp()

# if the scripts gets restarted we need to figure out when the next scheduled function call is 
# so we need to nkow when we started
curr_time = datetime.datetime.now()
curr_stamp = curr_time.timestamp()
last_interval = (curr_stamp - start_stamp) // desired_time_delta_in_seconds


# Loop forever
while(True):
    # sleep so we don't use up all the servers processing power just checking the time
    time.sleep(5)

    # get the current time and see how many whole intervals have elapsed
    curr_time = datetime.datetime.now()
    curr_stamp = curr_time.timestamp()
    num_intervals = (curr_stamp - start_stamp) // desired_time_delta_in_seconds # python 3 integer division

    # if at least another interval has elapsed and it is currently the time for the next call
    # then call the function and update the interval count so we don't call it again
    if (num_intervals > last_interval) and (curr_time.hour >= desired_time_hours) and (curr_time.minute >= desired_time_minutes):
        print("Calling scheduled function at ", curr_time)
        last_interval = num_intervals
        function_to_call_periodically()
Spoonless
  • 561
  • 5
  • 14
  • Thanks for the answer,actually i am running the script which contains other functions on a server every day at some time.I added your time.sleep function so will it work,as when the script will run other functions should run but this function should run every 3 days – user3254437 Aug 07 '18 at 08:46
  • No it will not work for multiple functions, unless you want to run all of them at evry 3 days. If you want to run them at different rates the answer by @GreenCloakGuy is much better. – Spoonless Aug 07 '18 at 09:05
  • how to run the script at a specific time? – user3254437 Aug 07 '18 at 10:48
  • You can sleep for 1 second, and then get the current time (using time.time). Then parse the current time in to year,month,day,hour,minute,second and then check if the the current time is the right time to run your function, and if so run it. Read more in the docs [here](https://docs.python.org/3/library/time.html) – Spoonless Aug 07 '18 at 11:07
  • But here if i am running the script on server at 11am everyday.I just want one function to be ran every other 3 day even the script is initiated to run every day at 11am.Can i do this by @GreenCloakGuy method? Again thanks for your time – user3254437 Aug 07 '18 at 14:31
  • I have updated my answer to give a complete code snippet to run the code every 3 days (from a specified starting date) at a specified time. – Spoonless Aug 07 '18 at 15:28