0

I have the following code:

import os, fcntl

f = open('./testfile.t', 'wb')
f.write(os.urandom(64))

fcntl.flock(f.fileno(), fcntl.LOCK_EX)
print 'Locked'

try:
    f2 = open('./testfile.t', 'wb')
except IOError as err:
    print err.errno
    print err

The idea is that second open should return an error, because file is still locked. The problem is it doesn't. Moreover, if I try to acquire a lock on f2.fileno() this code just hangs up, instead of throwing an exception. What do I do wrong?

sotona
  • 1,731
  • 2
  • 24
  • 34
  • Flocking is *advisory*. It doesn't make the file inaccessible, it merely prevents others from getting a lock on it simultaneously. Both processes must cooperate in the flocking. – deceze Oct 25 '17 at 12:36
  • @deceze so it's even more unclear why I cannot acquire second lock on this file and the process just hangs up – sotona Oct 25 '17 at 12:44
  • 4
    I don't see where you're trying to acquire a second lock, and if there's already an exclusive lock on the file, that *perfectly* explains why you can't acquire a second one. – deceze Oct 25 '17 at 12:45

0 Answers0