File.write() not working in a sleepy loop

Gavin Lusby gavinlusby at googlemail.com
Tue Feb 19 08:50:45 EST 2008


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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080219/140fb86c/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: load.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20080219/140fb86c/attachment.ksh>


More information about the Python-list mailing list