Hello, I am getting odd behaviour when I am trying to write a file in a while loop with a sleep function call.
I am trying to periodically change the number of jobs on a cluster depending on the current usage and a dummy version that does not require access to the cluster usage is attached at the end. The crux of the matter is in the main loop. When I run it, it writes a blank file on the first iteration, then sleeps and on the 2nd iteration, it finally puts in the correct value. I simply can't understand why it postpones the open, write, close section of the 1st loop until the sleep is finished. The sleep() is clearly after the write(). Thanks in advance, Gavin ___sample_code___sample_code___sample_code___sample_code___sample_code___sample_code___sample_code while i == 1: if pid <> -1: i = pid_exists(pid) if g.verbose: os.system('clear') ############################## get data ############################### freeload, halfload, fullload = 20,20,40#getnodedata()<----------------------- dummy data weekend, phase = gettimedata() userload = 2#getuserdata()<-------------------------------------------------- dummy data if g.verbose: print '...total nodes =',2 * (freeload + halfload + fullload) if g.verbose: print '...used nodes =',halfload + 2 * fullload if g.verbose: print '...free nodes =',2 * freeload + halfload if g.verbose: print '...user nodes =',userload ############################# usage logic ############################# if weekend or phase == 3: answer = maxnode if g.verbose: print '...maxing out the machine with '+str(answer) else: answer = max(minnode,2*freeload+halfload-slack) if g.verbose: print '...limiting runs to '+str(answer)+' nodes' ############################ write outfile ############################ o=open(os.getcwd()+g.dsep+file, 'w') if g.verbose: print '...writing to '+os.getcwd()+g.dsep+file o.write('%over = (njobs => '+str(answer)+');\n') o.close ############################### sleep now ############################# sleep(refresh)
#! /data/gen27/utg/lusbg1/bin/Python-2.5.1/python import sys, os, getopt, datetime from time import sleep from subprocess import Popen, PIPE from getpass import getuser class g: ''' global variables class ''' if os.name in ('nt','dos','ce'): dsep = '\\' else: dsep = '/' verbose = False def main(): ''' main iterative subroutine ''' if g.verbose: os.system('clear'); print '\n...loadmax.py' ############################### get opts ############################## try: opts, args = getopt.getopt(sys.argv[1:], "o:p:r:l:u:s:thv", ["help","verbose"]) except getopt.GetoptError: usage() sys.exit() file = 'mybad' slack = 20 minnode = 40 maxnode = 100 refresh = 10*60 tracker = 'tracker.dat' answer = 0 pid = -1 for opt, arg in opts: if opt == "-v": # verbosity switch g.verbose = True if opt == "-o": # output filename switch file = arg if opt == "-t": # tracker filename switch tracker = True if opt == "-r": # refresh rate converted min->sec refresh = float(arg)*60.0 if opt == "-u": # maximum permissable nodes maxnode = int(arg) if opt == "-l": # minimum permissable nodes minnode = int(arg) if opt == "-p": # follow specified pid pid = int(arg) if opt == "-s": # no of slack nodes slack = int(arg) if opt in ("-h", "--help"): # help! usage() sys.exit() if file == 'mybad': print '...error: output file not specified' usage() sys.exit() i = 1 while i == 1: if pid <> -1: i = pid_exists(pid) if g.verbose: os.system('clear') ############################## get data ############################### freeload, halfload, fullload = 20,20,40#getnodedata() weekend, phase = gettimedata() userload = 2#getuserdata() if g.verbose: print '...total nodes =',2 * (freeload + halfload + fullload) if g.verbose: print '...used nodes =',halfload + 2 * fullload if g.verbose: print '...free nodes =',2 * freeload + halfload if g.verbose: print '...user nodes =',userload ############################# usage logic ############################# if weekend or phase == 3: answer = maxnode if g.verbose: print '...maxing out the machine with '+str(answer) else: answer = max(minnode,2*freeload+halfload-slack) if g.verbose: print '...limiting runs to '+str(answer)+' nodes' ############################ write outfile ############################ o=open(os.getcwd()+g.dsep+file, 'w') if g.verbose: print '...writing to '+os.getcwd()+g.dsep+file o.write('%over = (njobs => '+str(answer)+');\n') o.close ############################### sleep now ############################# sleep(refresh) return def pid_exists(pid): ''' int = pid_exists(pid) returns 1 if the pid integer exists, otherwise 0 not brilliant as it's not so platform independent ''' try: os.kill(pid, 0) return 1 except OSError, err: return 0 def gettimedata(): ''' weekend, phase = gettimedata returns whether today is a weekend or flex and whether it is out of office hours ''' day = 'weekday' weekend = False hr = datetime.datetime.today().hour if hr >= 8 and hr <= 12: phase = 1; time = 'morning' elif hr > 12 and hr <= 18: phase = 2; time = 'afternoon' else: phase = 3; time = 'but outside hours' if datetime.datetime.today().weekday() == 4 and phase >= 2: weekend = True day = 'flex-friday' if datetime.datetime.today().weekday() >= 5: weekend = True day = 'weekend' if g.verbose: print '...it is a '+day+' '+time return weekend, phase def getuserdata(): ''' userjobs = getuserdata() returns the number of jobs that belong to the user ''' if g.verbose: print '...obtaining usage for user '+getuser() p1 = Popen(['qstat','-u',getuser()], stdout=PIPE) p2 = Popen(['grep','-c','compute-0-'], stdin=p1.stdout, stdout=PIPE) return int(p2.communicate()[0]) def getnodedata(): ''' freenodes, halfnodes, fullnodes = getnodedata() returns a tuple of the node counts for the 3 categories ''' p1 = Popen(['qstat','-f'], stdout=PIPE) p2 = Popen(['grep','-c','BIP 2/2'], stdin=p1.stdout, stdout=PIPE) p1 = Popen(['qstat','-f'], stdout=PIPE) p3 = Popen(['grep','-c','BIP 1/2'], stdin=p1.stdout, stdout=PIPE) p1 = Popen(['qstat','-f'], stdout=PIPE) p4 = Popen(['grep','-c','BIP 0/2'], stdin=p1.stdout, stdout=PIPE) return int(p4.communicate()[0]), int(p3.communicate()[0]), int(p2.communicate()[0]) def usage(): print 'USAGE: loadmax.py -o over.txt [options]' print print print print 'Options:' print ' -h = print help message and exit' print ' -l = lower limit nodes' print ' (default: 40)' print ' -u = upper limit nodes' print ' (default: 100)' print ' -s = no of slack nodes' print ' (default: 20)' print ' -r = refresh rate' print ' (default: 10mins)' print ' -p [pid] = end with given pid' print ' (default: 10mins)' print ' -v --verbose = turn on extra debug information' print ' (default: off)' return if __name__ == "__main__": main()
-- http://mail.python.org/mailman/listinfo/python-list