1

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.

closed as off-topic by lenik, Piotr Kula, Jivings May 1 '14 at 16:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – lenik, Piotr Kula, Jivings
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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
3

try this:

import time

start = time.time()

PERIOD_OF_TIME = 300 # 5min

while True :
    ... do something

    if time.time() > start + PERIOD_OF_TIME : break

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