Vindrue
Vindrue

Reputation: 93

Is there a way in Python to create a for loop that doesn't wait for the code inside to complete before iterating again?

Say i have a for loop with a time.sleep function inside

import time

list = [1, 2, 3]

for i in list:
    FunctionThatDoesSomethingTimeRelated(i)
    time.sleep(3)
    FunctionThatDoesSomethingTimeRelated(i)

Is there a way for me to make the loop iterate over every part of the list simultaneously instead of waiting for the code inside the loop to be executed?

Basically the execution order needs to be like this:

FunctionThatDoesSomethingTimeRelated(1)
FunctionThatDoesSomethingTimeRelated(2)
FunctionThatDoesSomethingTimeRelated(3)
time.sleep(3)
FunctionThatDoesSomethingTimeRelated(1)
FunctionThatDoesSomethingTimeRelated(2)
FunctionThatDoesSomethingTimeRelated(3)

Instead of this:

FunctionThatDoesSomethingTimeRelated(1)
time.sleep(3)
FunctionThatDoesSomethingTimeRelated(1)
FunctionThatDoesSomethingTimeRelated(2)
time.sleep(3)
FunctionThatDoesSomethingTimeRelated(2)
FunctionThatDoesSomethingTimeRelated(3)
time.sleep(3)
FunctionThatDoesSomethingTimeRelated(3)

Upvotes: 1

Views: 315

Answers (4)

tjallo
tjallo

Reputation: 791

Please be more clear in your questioning, you are currently using the same function name to describe (presumably) 2 different functions.

Also using hard coded timing to wait for completion generally isn't a good idea.

You could just use 2 for loops to get the abovementioned result.

import time

list1 = [1, 2, 3]

for i in list1:
    timeRelFunction1(i)

time.sleep(3)

for i in list1:
    timeRelFunction2(i)
   

Also using list as a variable name here isn't the proper way since it is a built-in class.

Edit according to comment:

You could try using something like this, check the stock prices every 3 seconds for n times

import time

list1 = [1, 2, 3]

def stockPriceEveryThreeSeconds(number_of_repetitions):
    for i in range(number_of_repetitions):
        for i in list1:
            getStockPrices(i)
        time.sleep(3)

stockPriceEveryThreeSeconds(2)

The above code will check the stockprice every 3 seconds, 2 times.

Upvotes: 2

Xinthral
Xinthral

Reputation: 437

Based on the question I'm reading you're looking for a bit of concurrency on list operations. There are a few ways to go about this, and the most optimal is by far the most complex. But an easy way to get started is with the threading module. You could modify the following code in this manner:

From:

import time

list = [1, 2, 3]

for i in list:
    FunctionThatDoesSomethingTimeRelated(i)
    time.sleep(3)
    FunctionThatDoesSomethingTimeRelated(i)

To:

def functionThatThreads(numberOfLoops):
    import time
    import threading
    list = [1, 2, 3]

    for _ in range(numberOfLoops):
        for i in list:
            threading.Thread(target=FunctionThatDoesSomethingTimeRelated, args=(i,)).start()
        time.sleep(3)

def FunctionThatDoesSomethingTimeRelated(i):
    print(i)

functionThatThreads(2)

Output:

 python .\testing.py
1
2
3
1
2
3

This will ensure that the execution order is maintained (first in first executed), while the output will be first finished, first served. Hopefully I answered the question, or this helps!

Upvotes: 0

Baz
Baz

Reputation: 13145

Is this what you want to do?

import time

list = [1, 2, 3]

for i in list:
    FunctionThatDoesSomethingTimeRelated(i)

time.sleep(3)

for i in list:    
    FunctionThatDoesSomethingTimeRelated(i) 

Upvotes: 1

Reblochon Masque
Reblochon Masque

Reputation: 36702

You could use a nested for loop:

for _ in range(10):     # how many times do these 3 function calls happen?
    for idx in range(3):
        FunctionThatDoesSomethingTimeRelated(idx)
    time.sleep(3)

Upvotes: 2

Related Questions