6

In Python's time module, there is a sleep() function, where you can make Python wait x seconds before resuming the program. Is there a way to do this indefinitely until a condition is met? For example:

while True:
    time.sleep()
    if x:
        break

time.unsleep()

I am trying to make a pause function for my PyGame program. Any help is appreciated.

vkumar
  • 863
  • 4
  • 9
  • 14
  • 2
    Does the thread *have* to sleep? From what it looks like, it doesn't have to - an infinite loop until the condition is met should suffice. – Zizouz212 May 24 '16 at 00:33
  • One way to pause (better than the type you would get from `sleep()`) is by setting a flag (`paused = True`) and not updating the game’s state `if paused`. – Ry- May 24 '16 at 00:34
  • 2
    @Zizou212 that would cause the game to use 100% CPU while paused, which is rude (makes the computer slow and hot, uses electricity, and drains the battery - all for no reason). At least include a short sleep in the loop then. – marcelm May 24 '16 at 13:10
  • Some relevance to answers at: https://stackoverflow.com/q/20170251/1959808 – 0 _ Apr 11 '18 at 21:42

3 Answers3

5

Something like this:

while not x: time.sleep(0.1)

will wait until x is true, sleeping a tenth of a second between checks. This is usually short enough for your script to seem to react instantly (in human terms) when x becomes true. You could use 0.01 instead if this is not quick enough. In my experience, today's computers are fast enough that checking a simple condition even every hundredth of a second doesn't really make a dent in CPU usage.

Of course, x should be something that can actually change, e.g. a function call.

kindall
  • 178,883
  • 35
  • 278
  • 309
4

Your code in the question implies that you want some other thread to resume your program. In that case you could use resumed = threading.Event(). You could create it in one thread and pass it into another:

while not resumed.wait(): # wait until resumed
    "continue waiting"

Call resumed.set() to resume this code immediately.

I am trying to make a pause function for my PyGame program. Any help is appreciated.

Use pygame.time. Typically, you have the main loop where you update the state of the game and at the end of the loop you call clock.tick(60) # 60 fps. It is enough to use paused flag in this case, to skip the updating.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 1
    A variation would be `forever = threading.Event(); forever.wait()`. This would be useful for a process whose only purpose is to handle interrupts (by setting handlers via [`signal`](https://docs.python.org/3/library/signal.html)). See also: https://groups.google.com/d/msg/gevent/Abtxtz3aZmQ/vrS7kTCqVY0J – 0 _ Apr 11 '18 at 21:49
0

You could spin off a thread as follows:

import sys    
import time
from threading import Thread

prepare_to_stop = 0

def in_background_thread():
    while not prepare_to_stop:
        # Program code here
        print(prepare_to_stop)
        time.sleep(0.1)

try:
    th = Thread(target=in_background_thread)
    th.start()
    print("\nProgram will shut down after current operation is complete.\n")
    time.sleep(10**8)
except KeyboardInterrupt:
    prepare_to_stop = 1

print("Program shutting down...")
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147