Skip to content

Commit 7f987f0

Browse files
committed
add func for get lockfile use tmepfile.
1 parent f8f4d9e commit 7f987f0

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

utility.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import stat
1515
import shlex
1616
import select
17+
import tempfile
18+
import fcntl
1719

1820
try:
1921
import json
@@ -576,6 +578,19 @@ def jsonify(result, format=False):
576578
except UnicodeDecodeError:
577579
return json.dumps(result2, sort_keys=True, indent=indent)
578580

581+
def log_lockfile(prog):
582+
# create the path for the lockfile and open it
583+
tempdir = tempfile.gettempdir()
584+
uid = os.getuid()
585+
path = os.path.join(tempdir, "%s.lock.%s" % (prig, uid))
586+
lockfile = open(path, 'w')
587+
# use fcntl to set FD_CLOEXEC on the file descriptor,
588+
# so that we don't leak the file descriptor later
589+
lockfile_fd = lockfile.fileno()
590+
old_flags = fcntl.fcntl(lockfile_fd, fcntl.F_GETFD)
591+
fcntl.fcntl(lockfile_fd, fcntl.F_SETFD, old_flags | fcntl.FD_CLOEXEC)
592+
return lockfile
593+
579594
if __name__ == '__main__':
580595
#configvalue = getConfig("./config.ini", "mysql", "port")
581596
#print configvalue

0 commit comments

Comments
 (0)