python-simpleflock icon indicating copy to clipboard operation
python-simpleflock copied to clipboard

I have found a problem that synchronization is not achieved.

Open ahan-ai opened this issue 8 years ago • 1 comments

I try to use python-simpleflock in my project, but I find it can not provide the synchronization feature as I think it should.

I have two files: simple_flock.py, simple_flock2.py. The source code of simple_flock.py is:

import os
import sys
import time

import simpleflock

while True:
    with simpleflock.SimpleFlock("/tmp/haha.lock"):
        print "time begin:", time.strftime("%H:%M:%S", time.localtime())
        print "haha"
        time.sleep(10)
        print "time end:", time.strftime("%H:%M:%S", time.localtime())
    time.sleep(1)

The source code of simple_flock2.py is almost the same as simpel_flock.py, except that it print "haha2" instead.

I run simple_flock.py firstly, and then simple_flock2.py.

Here is the result: The simple_flock.py will hold the lock first, and then the simple_flock2.py have to wait about 10 seconds to get the lock, but then two processes just keep running like no synchronization. Because I can see each process keep printing "haha" or "haha2" every 10 seconds.

I read the source code, I found that if I remove the code

      try:
         os.unlink(self._path)
      except:
         pass

in the def exit(self, *args) function, the PR can be solved.

My test envrionment is Centos 6.5, Python version is 2.6.6.

ahan-ai avatar Jun 30 '17 11:06 ahan-ai

Thanks for this. I'm seeing similar behavior with python 2.7.3 and a Debian based OS.

Removing the unlink appears to solve this issue.

ghost avatar Jun 25 '18 18:06 ghost