Python: Running a loop for a specific duration

I need to run a cycle for a certain duration. For example, the code I'm trying to do will look like this:

initialize and lauch timer while timer<10000sec: do things 

Do you know how to do this?

Thanks:)

+4
source share
1 answer
 stop = time.time()+10000 while time.time() < stop: do things 

This requires that each run through the cycle be short enough so that you read the current time often enough (to ensure the accuracy you want to achieve).

+6
source

Source: https://habr.com/ru/post/1333654/


All Articles