I would like to add a timing feature into my python code. I have a situation where data reading occurs continuously and rapidly. How do I implement a timing system to stop reading after a period of time? Time.sleep() doesn't help in this case as it only pauses the program for a certain period of time. What I need to implement is to force stop the program (stop reading/close file) after a certain period of time. Any help is much appreciated.

  • Hi there. I'm putting your question on-hold as it does not appear to be specific to the Raspberry Pi within the scope defined in the help center. – Jivings May 1 '14 at 16:21

try this:

import time

start = time.time()

PERIOD_OF_TIME = 300 # 5min

while True :
    ... do something

    if time.time() > start + PERIOD_OF_TIME : break
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.