0
from time import sleep

def foo():
    sleep(3)
    return True

while True:
    print('Running')

    if foo() == True:
        print('Finished.')
        break

I want to keep printing "Running" but when foo returns True I want to print "Finished" (once) and break out of the loop.

I have tried the above but it prints "Running" just once and waits for foo to finish executing and then continues.

Sakib
  • 3
  • 1
  • 2
  • Does this answer your question? [How would I stop a while loop after n amount of time?](https://stackoverflow.com/questions/13293269/how-would-i-stop-a-while-loop-after-n-amount-of-time) – SuperStormer Jun 28 '22 at 19:56
  • 3
    You'll need to look into multiprocessing, threading, or async/await; those are usually how people do concurrent tasks in Python. Have you looked up any of those? Or maybe you instead just need to check continuously in your loop to see if a certain amount of time has passed since it started? In that case you'd just get the date/time that you started the loop, and check that the current date and time is not more than, say, 3 seconds ahead of the start time. Just depends on what you actually need - so it would help to clarify. – Random Davis Jun 28 '22 at 19:56
  • @RandomDavis I thought it would require threading. But not sure how to combine (interact) two threads together. I have used threading but only to execute two functions simultaneously (parallelly). – Sakib Jun 28 '22 at 20:04
  • From your code it looks like it'd be more straightforward to use `async`/`await`. But what you're asking for is an extremely simple and common use case; surely there's tons of examples out there on how to do related things. – Random Davis Jun 28 '22 at 20:05

2 Answers2

0

So, after trying somethings I found 2 solutions, of my own question, that uses threading.

1. Modifies the foo function

from time import sleep
from threading import Thread

x = True
def foo():
    sleep(3)

    global x
    x = False
    print('finished')


def printing():
    while x:
        print('Running')


foo_thread = Thread(target=foo)
foo_thread.start()

printing_thread = Thread(target=printing)
printing_thread.start()

2. Uses decorator to keep foo unchanged

from time import sleep
from threading import Thread

x = True
def sets_true(func):

    def wrapper():
        returned_value = func()
        global x
        x = False

        print('finished')

    return wrapper

@sets_true
def foo():
    sleep(3)
    return True


def printing():
    while x:
        print('Running')


foo_thread = Thread(target=foo)
foo_thread.start()

printing_thread = Thread(target=printing)
printing_thread.start()
Sakib
  • 3
  • 1
  • 2
0
import threading
from time import sleep


flag = True


def foo()->None:
    global flag
    sleep(1)
    flag = False


if __name__ == "__main__":
    t1 = threading.Thread(target=foo)
    t1.start()
    while flag:
        print('Running')
    print('Finished')

Because you worked with only one thread, when you call the function the main stops until the function returns. Therefore, if you want the main code and the function to run together, you have to work with threads.

Noy Hanan
  • 16
  • 1