简体   繁体   中英

Tell Python to wait/pause a 'for' loop

I have a Python program that navigates me to a website with a function that I have defined as nav(a, b), and on this site I will be downloading some pyfits data for use on another script. This site has a different pyfits file for every set of (a,b) in a catalog I have.

I was wondering if I could iterate through this catalog using a for loop, and each time the nav(a, b) function is used, tell python to pause while I download the file, then resume again when I tell it to. I've done something like this in IDL before, but don't know how with Python.

Otherwise I guess I'm stuck running the program 200 times, replacing the (a, b) values each time, which will take for ever.

If you want to wait for a manual signal to continue, wait for the user to press Enter :

Python 2:

raw_input("Press Enter to continue...")

Python 3:

input("Press Enter to continue...")

If you can download the file in the python code, do that instead of doing the manual task for each of the files.

You can use time.sleep() to pause the execution for t seconds:

import time
time.sleep(1.3) # Seconds

Demo:

import time

print "Start Time: %s" % time.ctime()
time.sleep(5)
print "End Time: %s" % time.ctime()

Output

Start Time: Tue Feb 17 10:19:18 2009
End Time: Tue Feb 17 10:19:23 2009

Use a while loop, waiting for your download to finish:

for ... :
    nav(a,b)
    while downloading_not_finished:
         time.sleep(X)

So, every X period of time a condition is tested, and is tested again until the downloading part is finished.

Okay, here are two ways to pause in Python.

  1. You can use the input function.

     # Python 2 raw_input("Downloading....") # Python 3 input("Downloading....")

This will pause the program until the user presses Enter , etc.

  1. You can use the time.sleep() function.

     import time time.sleep(number of seconds)

This will pause the Python script for however many seconds you want.

For Python shell in design:

import sys
from time import sleep

try:
    shell = sys.stdout.shell
except:
    print('Run It In Shell')
dots = '........';
shell.write('Downloading')
sleep(0.5)
for dot in dots:
    shell.write(dot)
    sleep(0.1)
shell.write('\n')
sleep(0.4)
shell.write('Saving Files')
sleep(0.5)
for doot in dots:
    shell.write(dot)
    sleep(0.1)
shell.write('\n')
sleep(0.4)

For Python in a console:

from time import sleep

print('Downloading')
sleep(1)
print('Saving Files')
sleep(1)

Use the input function. It will pause the program until the prompt has been closed/filled in.

The best solution is to put another while loop inside the existing while loop:

while True:
#do something
asleep = True
while (asleep):
    if cv2.waitKey(1) == 32: #Wait for space key  
        asleep = False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM