flock() question

Carsten Gaebler cg at schlund.de
Tue Feb 6 05:11:53 EST 2001


Hi there!

It seems I don't understand file locking. In the following example the
parent process locks a file and then the child process locks the same file
but does not wait until the parent unlocks it. What am I doing wrong?

import fcntl, FCNTL, os, time

f = open("/tmp/locktest", "w")

pid = os.fork()
if pid == 0:
        time.sleep(1)
        print "Child: locking file"
        fcntl.flock(f.fileno(), FCNTL.LOCK_EX)
        print "Child: file locked"
        time.sleep(5)
        os._exit(0)

else:
        print "Parent: locking file"
        fcntl.flock(f.fileno(), FCNTL.LOCK_EX)
        print "Parent: file locked"
        time.sleep(10)
        fcntl.flock(f.fileno(), FCNTL.LOCK_UN)



Regards
Carsten.



More information about the Python-list mailing list