title: Complete a task during certain time frames within a python script
body: <p>As you can see below here is a copy of my script what I've created, could someone help me improve/fix my <code>currenttime</code> tasks.</p>
  
  <p>Basically, during the day periodically calls test every 3 mins +- 60 seconds each periodically cycle it is supposed to do the follow tasks:</p>
  
  <ul>
  <li>Anywhere during 23:30:00 to 23:40:00 it turns clearscreen to false</li>
  <li>Anywhere during 23:40:00 to 23:50:00 it checks if clearscreen is false and if it is then it clears certain files and then it immediately sets clearscreen to false</li>
  <li><p>Anywhere during 23:50:00 to 01:30:00 it just idles, doing nothing other than checking the time.</p>
  
  <pre><code>from datetime import date, timedelta
  from sched import scheduler
  from time import time, sleep, strftime
  import random
  clearscreen = 0
  
  def periodically(runtime, intsmall, intlarge, function):
      global clearscreen
  
       ## Get current time
      currenttime = strftime('%H:%M:%S')
  
      ## while currenttime is anywhere between 23:40 and 23:50 then...
      while currenttime &gt; '23:30:00' and currenttime &lt; '23:40:00':
          ## Update time
          currenttime = strftime('%H:%M:%S')
          print ("""23:30:00 to 23:40:00 | %s""" % (currenttime))
          sleep(1)
  
          ## If clearscreen = false then...
          if clearscreen == False:
              ## Set clearscreen to true to enable the next part work and to disable this part until the next day.
              clearscreen = True
              print "changed to true"
  
      ## If currenttime is anywhere between 23:50 and 23:59 then...
      while currenttime &gt; '23:40:00' and currenttime &lt; '23:50:00':
          ## Update time
          currenttime = strftime('%H:%M:%S')
          print ("""23:40:00 to 23:50:00 | %s""" % (currenttime))
          sleep(1)
  
          if clearscreen == True:
              ## clear stuff here
              print "cleared"
              ## Change clearscreen to to stop it running
              clearscreen = False
              print "changed to false"
  
      ## Only allow run_periodically during 01:30 and 23:30
      if clearscreen == False:
          while currenttime &gt; '23:50:00' and currenttime &lt; '23:59:59':
              ## Update time
              currenttime = strftime('%H:%M:%S')
              print ("""23:50:00 to 23:59:59 | %s""" % (currenttime))
              sleep(1)
          while currenttime &gt; '00:00:00' and currenttime &lt; '01:30:00':
              ## Update time
              currenttime = strftime('%H:%M:%S')
              print ("""00:00:00 to 01:30:00 | %s""" % (currenttime))
              sleep(1)
  
      runtime += random.randrange(intsmall, intlarge)
      s.enter(runtime, 1, function, ())
      s.run()
  
  def test():
      print "test function"
  
  while True:
      periodically(180, -60, +60, test)
  </code></pre></li>
  </ul>
  
  <p>Any help would be much appreciated.</p>
  
  <h2>Edit for @abarnert</h2>
  
  <p>In regards to the loops this is what the function is supposed to be doing:</p>
  
  <pre><code>23:3x:xx - While loop initiated as currenttime fits the while loop's conditions (time is inbetween 23:30:00 and 23:40:00)
  23:3x:xx - clearscreen is false, setting it to true.
  23:3x:xx to 23:40:00 - currenttime is updated every 1 second(s)
  23:40:01 - While loop ended as currenttime no longer fits the while loop's conditions (time is outside of 23:30:00 and 23:40:00)
  
  23:40:01 - While loop initiated as currenttime fits the while loop's conditions (time is inbetween 23:40:00 and 23:50:00)
  23:40:01 - clearscreen is true, doing some stuff and then changing clearscreen to false
  23:40:01 to 23:50:00 - currenttime is updated every 1 second(s)
  23:50:01 - While loop ended as currenttime no longer fits the while loop's conditions (time is outside of 23:40:00 and 23:50:00)
  
  23:50:01 - While loop initiated as currenttime fits the while loop's conditions (time is inbetween 23:50:00 and 23:59:59)
  23:50:01 to 23:59:59 - currenttime is updated every 1 second(s)
  00:00:00 - While loop ended as currenttime no longer fits the while loop's conditions (time is outside of 23:50:00 and 23:59:59)
  
  00:00:00 - While loop initiated as currenttime fits the while loop's conditions (time is inbetween 00:00:00 and 01:30:00)
  00:00:00 and 01:30:00 - currenttime is updated every 1 second(s)
  00:00:00 - While loop ended as currenttime no longer fits the while loop's conditions (time is outside of 00:00:00 and 01:30:00)
  </code></pre>
  
  <hr>
  
  <p>You mention that I repeatedly update currenttime, then print, then sleep. How would I get around this "problem"?</p>
  
  <hr>
  
  <p>In regards to the <code>global clearscreen</code> I am unsure what you mean by; "you don't have a lock on it"</p>
  
  <hr>
  
  <p>I'm running Windows so signal.setitemer is a no go and also I need certain values/variables to be stored in memory for my script so scheduled tasks on windows won't be appropriate correct?</p>
  
  <p>You built an example bit of code using the <code>sched</code> module but it doesn't work and I am unable to work out how to get it working, plus it's also rather confusing for me. I'm still learning and it's rather confusing.</p>