1

I want to keep the loop conditional statement running, but do not always check the conditions.

For example, if the condition is true, then in the next 3 seconds, the loop's conditional statement will run, and then check the condition after the 3rd second, then repeat this process.

I don't want to wait or sleep for three seconds, I want my loop to do work for three seconds. And then check if it should continue for another three as mentioned by @RemcoGerlich

while if_active() == True:    #check the condition every 3 seconds` 
   try:               # it will keep running in 3 seconds if if_active() is true  
       with open(masterpath, 'r') as f:
            s = f.read()
        exec(s)
Andy
  • 101
  • 1
  • 1
  • 7
  • Please fix your indentation. – Daniel Roseman Mar 05 '19 at 15:09
  • There's no point in waiting 3 seconds unless you use something from the OS that will put your process to sleep. Otherwise it will continue to eat the CPU even while you wait. – Mark Ransom Mar 05 '19 at 15:11
  • Are you saying that reading the file and `exec(s)` takes three seconds to run? – 9769953 Mar 05 '19 at 15:12
  • It's more idiomatic to replace `while if_active() == True:` by `while if_active():`. – 9769953 Mar 05 '19 at 15:13
  • I feel everybody is misreading his question. He doesn't want to wait or sleep for three seconds, he wants his loop to do work for three seconds. And then check if it should continue for another three. – RemcoGerlich Mar 05 '19 at 15:23
  • The question is unclear; you should clarify what they want: wait 3 seconds after `exec(s)`; check the loop condition every 3 seconds whatever the duration of `exec(s)`; something else. Please edit your question to clarify what you want. – 9769953 Mar 05 '19 at 15:39

3 Answers3

0

Here's a fun and async way. Just for fun, with a demo for activateing

import signal, os
import time

def handler(signum, frame):
    for i in range(3):
        print("foo bar")
        time.sleep(0.1)
    signal.alarm(3)

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(3)

while True:
    try:
        active = not active
        if not active:
            signal.alarm(0)
        time.sleep(60)
    except KeyboardInterrupt as interrupt:
        # demonstrating activate, with ctrl+c
        signal.alarm(3)
han solo
  • 6,390
  • 1
  • 15
  • 19
-1

You can use a command like sleep to avoid running continuously. You can see a more explained answer in this thread: How can I make a time delay in Python?

  • 1
    `time.sleep()` is, of course, fine, but if `exec(s)` takes (randomly) anywhere between 0 and 3 seconds to run, the total loop time becomes between 3 and 6 seconds (and all bets are off if `exec(s)` takes longer than 3 seconds). Unfortunately, the OP has failed to clarify what exactly they want, making this question ambiguous, and all answers could be correct. – 9769953 Mar 05 '19 at 15:37
-1

You can track when the last time was that you did the check, and only re-do the check if three seconds have passed.

from datetime import datetime, timedelta

INTERVAL = timedelta(minutes=3)
last_checked = datetime.now() - INTERVAL

while True:
    now = datetime.now()
    if last_checked <= (now - INTERVAL):
        if not if_active():
            break
        last_checked = now

    # do your thing here
    pass

This could use some refactoring but the idea should work.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79