fcntl.flock() not working when called from a function

thakadu thakadu at gmail.com
Fri Nov 4 14:33:39 EST 2005


The following code works as expected when run in the main body of a
python script (ver 2.3.5) on OpenBSD v3.8. but when it is in the body
of a function definition it does not work. It does not raise any errors
but it simply does not block as expected. I have repeated this in both
a cgi envirnoment and a non-cgi environment. I also have repeated it
under OpenBSD 3.6 Python Version 2.3.4. To test this you need to run
the code from two shells or if testing the cgi version from two
browsers.
The first call to flock() should pass without blocking while the second
call should block.
I have tried varying the file open mode by using 'r', 'r+', 'w', 'rw',
'w+', and always I get the same results.
Does anyone have an idea why this would work in the main body of the
script but not in a function?

This code works if in the main body of a script:

import fcntl
f=open('/tmp/lockfile')
fcntl.flock(f,fcntl.LOCK_EX)

but this does not work:

import fcntl

def lock():
    f=open('/tmp/lockfile')
    fcntl.flock(f,fcntl.LOCK_EX)

lock()




More information about the Python-list mailing list