Tuesday, September 22, 2009

python - while loop specified time frame

import time

def looper(timeCount):
start = time.time()
keepLooping = True
while keepLooping:
if time.time()-start > timeCount:
keepLooping = False
else:
print time.time()-start
time.sleep(0.1)



if __name__ == '__main__':
looper(10.0)

2 comments:

  1. is there way to setup this while loop to run while the cpu is below a specified threshold? to keep it from monopolizing resources.
    experimenting with psutil - it works great with linux, but my backup scripts are resource hogs.

    ReplyDelete
  2. @geekbuntu - thanks for the question! I posted my solution to limiting the cpu here. I use the os module so it should be portable across several OSes. Enjoy!

    ReplyDelete