Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating a loop with a pause

Tags:

python

I am working to integrate with an API that has a limit on the number of requests per second. Is there a way, when running a for loop in python to delay each cycle? Conceptually, something like --

def function(request):
    for x in [a,b,c,d,...]:
        do something
        wait y seconds

Thank you.

like image 236
David542 Avatar asked Sep 06 '11 22:09

David542


People also ask

Is there a way to pause a loop in Python?

Adding a Python sleep() Call With time.sleep() Python has built-in support for putting your program to sleep. The time module has a function sleep() that you can use to suspend execution of the calling thread for however many seconds you specify.

Can you pause a for loop in JavaScript?

To create pause or delay in a JavaScript for loop, we should use await with a for-of loop. to define the wait function that returns a promise that calls setTimeout with resolve to resolve the promise in ms milliseconds. Then we define the loop function that runs a for-of loop to loop through an array.

What is a delay loop?

Time delay loops are often used in programs. These are loops that have no other function than to kill time. Delay loops can be created by specifying an empty target statement.

How do you wait for loop to finish?

To use Javascript promises in a for loop, use async / await . This waits for each promiseAction to complete before continuing to the next iteration in the loop.


1 Answers

import time
...
time.sleep(5)

This will sleep for 5 seconds. See http://docs.python.org/library/time.html#time.sleep

like image 51
Jim Garrison Avatar answered Sep 30 '22 13:09

Jim Garrison