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: