提交 b534bcf7 编写于 作者: S shenyuhan

fix block when add multi lock byself

上级 2c46bb2a
...@@ -19,6 +19,7 @@ from __future__ import print_function ...@@ -19,6 +19,7 @@ from __future__ import print_function
import six import six
import sys import sys
import os
from paddlehub.common.logger import logger from paddlehub.common.logger import logger
from paddlehub.common.utils import sys_stdin_encoding from paddlehub.common.utils import sys_stdin_encoding
...@@ -29,6 +30,8 @@ from paddlehub.commands import help ...@@ -29,6 +30,8 @@ from paddlehub.commands import help
from paddlehub.commands import version from paddlehub.commands import version
from paddlehub.commands import run from paddlehub.commands import run
from paddlehub.commands import download from paddlehub.commands import download
from paddlehub.common.lock import lock
from paddlehub.common.dir import CONF_HOME
class HubCommand(BaseCommand): class HubCommand(BaseCommand):
...@@ -66,7 +69,10 @@ def main(): ...@@ -66,7 +69,10 @@ def main():
argv.append(item.decode(sys_stdin_encoding()).decode("utf8")) argv.append(item.decode(sys_stdin_encoding()).decode("utf8"))
else: else:
argv.append(item) argv.append(item)
fp_lock = open(os.path.join(CONF_HOME, 'config.json'))
lock.flock(fp_lock, lock.LOCK_EX)
command.execute(argv[1:]) command.execute(argv[1:])
lock.flock(fp_lock, lock.LOCK_UN)
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -13,14 +13,34 @@ class WinLock(object): ...@@ -13,14 +13,34 @@ class WinLock(object):
class Lock(object): class Lock(object):
_owner = None
def __init__(self): def __init__(self):
self.LOCK_EX = fcntl.LOCK_EX
self.LOCK_UN = fcntl.LOCK_UN
self.LOCK_TE = ""
if os.name == "posix": if os.name == "posix":
self.lock = fcntl self.lock = fcntl
else: else:
self.lock = WinLock() self.lock = WinLock()
_lock = self.lock
def get_lock(self): def get_lock(self):
return self.lock return self.lock
def flock(self, fp, cmd):
lock = Lock().get_lock() if cmd == fcntl.LOCK_UN:
Lock._owner = None
self.lock.flock(fp, cmd)
elif cmd == fcntl.LOCK_EX:
if Lock._owner is None:
Lock._owner = os.getpid()
self.lock.flock(fp, cmd)
else:
if Lock._owner == os.getpid():
pass
else:
self.lock.flock(fp, cmd)
lock = Lock()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册