Timed method in Python
<p>How do I have a part of python script(only a method, the whole script runs in 24/7) run everyday at a set-time, exactly at every 20th minutes? Like 12:20, 12:40, 13:00 in every hour.</p>

<p>I can not use cron, I tried periodic execution but that is not as accurate as I would... It depends from the script starting time.</p>

<p>You can either put calling this method in a loop, which would sleep for some time </p>

<pre><code>import time
while True:
    sleep(1200)
    my_function()
</code></pre>

<p>and be triggered once in a while, you could use datetime to compare current timestamp and set next executions.</p>

<pre><code>import datetime

function_executed = False
trigger_time = datetime.datetime.now()

def set_trigger_time():
    global function executed = False
    return datetime.datetime.now() + datetime.timedelta(minutes=20)

while True:
    if function_executed:
        triggertime = set_trigger_time()

    if datetime.datetime.now() == triggertime:
        function_executed = True
        my_function()
</code></pre>

<p>I think however making a system call the script would be a nicer solution.</p>

python