55import time
66import logging
77from distutils .version import LooseVersion
8- import warnings
98
109import portalocker
1110
@@ -33,16 +32,12 @@ def __init__(self, lockfile_path):
3332 flags = portalocker .LOCK_EX | portalocker .LOCK_NB ,
3433 ** open_kwargs )
3534
36- def try_to_create_lock_file (self ):
37- """Do not call this. It will be removed in next release"""
38- warnings .warn ("try_to_create_lock_file() will be removed" , DeprecationWarning )
39- return self ._try_to_create_lock_file ()
40-
4135 def _try_to_create_lock_file (self ):
4236 timeout = 5
4337 check_interval = 0.25
4438 current_time = getattr (time , "monotonic" , time .time )
4539 timeout_end = current_time () + timeout
40+ pid = os .getpid ()
4641 while timeout_end > current_time ():
4742 try :
4843 with open (self ._lockpath , 'x' ): # pylint: disable=unspecified-encoding
@@ -51,15 +46,18 @@ def _try_to_create_lock_file(self):
5146 logger .warning ("Python 2 does not support atomic creation of file" )
5247 return False
5348 except FileExistsError : # Only Python 3 will reach this clause
54- logger .warning ("Lock file exists, trying again after some time" )
49+ logger .debug (
50+ "Process %d found existing lock file, will retry after %f second" ,
51+ pid , check_interval )
5552 time .sleep (check_interval )
5653 return False
5754
5855 def __enter__ (self ):
56+ pid = os .getpid ()
5957 if not self ._try_to_create_lock_file ():
60- logger .warning ("Failed to create lock file" )
58+ logger .warning ("Process %d failed to create lock file" , pid )
6159 file_handle = self ._lock .__enter__ ()
62- file_handle .write ('{} {}' .format (os . getpid () , sys .argv [0 ]).encode ('utf-8' ))
60+ file_handle .write ('{} {}' .format (pid , sys .argv [0 ]).encode ('utf-8' )) # pylint: disable=consider-using-f-string
6361 return file_handle
6462
6563 def __exit__ (self , * args ):
@@ -70,5 +68,5 @@ def __exit__(self, *args):
7068 # file for itself.
7169 os .remove (self ._lockpath )
7270 except OSError as ex : # pylint: disable=invalid-name
73- if ex .errno != errno .ENOENT and ex . errno != errno .EACCES :
71+ if ex .errno not in ( errno .ENOENT , errno .EACCES ) :
7472 raise
0 commit comments