# Pause and resume

**URL:** <https://discourse.psychopy.org/t/pause-and-resume/1771>\
**Category:** Coding\
**Created:** [January 18, 2017, 2:57pm UTC](https://discourse.psychopy.org/t/pause-and-resume/1771 "2017-01-18T14:57:56Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [February 15, 2017, 11:50pm UTC](https://discourse.psychopy.org/t/pause-and-resume/1771/5 "2017-02-15T23:50:23Z")

</div>

Something like this (where `images` is a list of 30 pre-created image stimuli):

```auto
image_presses = [] # keep track of which images were paused

for trial in trials: # on each trial,
    for image in images: # draw 30 images
        for frame in range(30): # for 30 frames each (= 500 ms at 60 Hz)
            # draw the stimulus:
            image.draw()
            win.flip()

            # check whether to pause
            if event.getKeys('space'):
                paused = True # stop right here
                image_presses.append(frame) # store the current image number 
                # or could append(image.image) to store the actual image name.

                while paused:
                    if event.getKeys('space'):
                       paused = False
                    else:
                        # need to let the rest of system time to do stuff in this tight loop 
                        time.sleep(0.001) # 1 ms per iteration should do
                else:
                    # when exiting the while loop, also terminate the enclosing 500 ms 
                    # for loop and go on to the next image:
                    break 

```

Note that as this code is based on drawing frames rather than keeping time, you can be paused indefinitely without issues.

---

_[View the full topic](https://discourse.psychopy.org/t/pause-and-resume/1771)._
