title: Python pause excution whenever a condition is met and resume where it left if condition is unmet again
body: <p>I have a memory greedy script and I dont want to freeze my computer while running it.
  I need to pause excution whenever the memory usage exceeds a limit lets say 60% and then get back where it left. I think this is not a very programming practice.</p>
  <p>lets say I have a code:</p>
  <pre><code>while True:
        do this stuff
        and this other stuff
        and this other ...
        ...
  </code></pre>
  <p>The only solution I know is to plague the code with many:</p>
  <pre><code>while psutil.virtual_memory().percent &gt;memory_limit:
        time.sleep(30) 
  </code></pre>
  <p>between lines, like this:</p>
  <pre><code>while True:
        while psutil.virtual_memory().percent &gt;memory_limit:
                time.sleep(30)
        do this stuff
        while psutil.virtual_memory().percent &gt;memory_limit:
                time.sleep(30) 
        and this other stuff
        while psutil.virtual_memory().percent &gt;memory_limit:
                time.sleep(30) 
        and this other ...
        while psutil.virtual_memory().percent &gt;memory_limit:
                time.sleep(30) 
        ...
  </code></pre>
  <p>what is not a nice code, the most aproximate solution to that would be</p>
  <pre><code>while not psutil.virtual_memory().percent &gt;memory_limit:
        do this stuff
        and this other stuff
        and this other ...
        ...
  else:
       time.sleep(30)
  </code></pre>
  <p>but this restarts the excution instead of resuming.</p>
  <p>Why python doesn't have an integrated whenever to do this.</p>