# It is sometimes useful to monitor how much cpu time or # cpu percentage your script is consuming. # This script will limit the cpu usage of your script # This example demostrates how to calculate the system # and user cpu time and cpu percentage # Note: this example is in python 3.0 # however, it is easily ported back to 2.x # by replaceing print() with print import os import time def getPercentage(unew, uold, start): """ calculate the percentage of cpu time """ return 100 * (float(unew) - float(uold)) / (time.time()-float(start)) def looper(timeCount, percentageGoal): """ loop over many tasks and keep the total cpu percentage consumtion to a desired level """ start = time.time() time.sleep(0.1) keepLooping = True uold, sold, cold, c, e = os.times() percentage = 0.0 while keepLooping: unew, snew, cnew, c, e = os.times() # since we are calculating the times from before we started looping the # percentages will be averaged over the duration of the script. print ("user %", percentage) # This just toggles to stop looping # when a time has been reached. In a real # script you would check for more work and # toggle off when there is no more work to # be done. if time.time()-start > timeCount: keepLooping = False #else: # print( time.time()-start) # do work: # In order for this script to actually help limit # the cpu usage you would need to break your script into # sections. # For example: if you were going to iterate through a large # list of data and perform actions on the contents # of the list then you should perform on action here # and keep looping through until all the actions # are accomplished. # # in this case we're just eating cpu so we get some numbers print("do work...") for i in range(1,1000000): b = 8*342*i*234 # tone back cpu usage while True: percentage = getPercentage(unew, uold, start) if percentage > percentageGoal: time.sleep(0.1) else: break; if __name__ == '__main__': # loop through work (for 4 seconds) and keep the cpu % # to less than 30% looper(4, 30) ## my output: ## user % 0.0 ## do work... ## user % 0.0 ## do work... ## user % 29.6673831301 ## do work... ## user % 29.1137166495 ## do work... ## user % 29.7617156875 ## do work... ## user % 29.5707887319 ## do work... ## user % 29.8122197706 ## do work... ## user % 29.3053848216 ## do work... ## user % 29.9385051866 ## do work...
A python example based blog that shows how to accomplish python goals and how to correct python errors.
Showing posts with label sleep. Show all posts
Showing posts with label sleep. Show all posts
Friday, August 20, 2010
Python - limit cpu percentage for script
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)
Wednesday, September 16, 2009
Python - sleep and random
# sleep for a random amount of time # here is another sleep example import time import random random.seed() for i in xrange(10): n = random.random() print str(i) + ": sleep for seconds: " + str(n) time.sleep(n) #output (for this run): #0: sleep for seconds: 0.611660870691 #1: sleep for seconds: 0.0712530683474 #2: sleep for seconds: 0.74601244734 #3: sleep for seconds: 0.942065303858 #4: sleep for seconds: 0.880462544493 #5: sleep for seconds: 0.315049974845 #6: sleep for seconds: 0.446147135853 #7: sleep for seconds: 0.306590705779 #8: sleep for seconds: 0.420561109255 #9: sleep for seconds: 0.950325171948
Subscribe to:
Posts (Atom)