BenjaminK
BenjaminK

Reputation: 773

Python - Run function every n second while true

I read my way through a lot of posts but couldn't find a solution for an else condition working. My loop sadly never stops. It seems like it's not repeatedly checking if project.IsInProgress() = True

I want to check all let's say every two seconds if my statement is still True, if it's no longer True I want to break the repetition and execute a print statement.

I guess the problem is that it's not running the function ever two seconds. But I don't know how to approach this.

check_status = project.IsInProgress()

while check_status:
    print('Render in progress..')
    time.sleep(2)
else: 
    print('Render is finished')

Upvotes: 0

Views: 1089

Answers (2)

Gabio
Gabio

Reputation: 9484

Your code checks the in-progress only once in the beginning of the code and if it's True, the loop will last forever. In order to check the status for each iteration , try:

while project.IsInProgress() :
    print('Render in progress..')
    time.sleep(2)
else: 
    print('Render is finished')

Upvotes: 0

João Castilho
João Castilho

Reputation: 487

Try this:

while project.IsInProgress():
    print('Render in progress..')
    time.sleep(2)
print('Render is finished')

Or if you prefer:

check_status = project.IsInProgress()
while check_status:
    print('Render in progress..')
    time.sleep(2)
    check_status = project.IsInProgress()
print('Render is finished')

Upvotes: 1

Related Questions