File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change 2
2
import os
3
3
import sys
4
4
import errno
5
+ import time
6
+ import logging
7
+
8
+ logger = logging .getLogger (__name__ )
9
+
5
10
import portalocker
6
11
from distutils .version import LooseVersion
7
12
@@ -25,7 +30,26 @@ def __init__(self, lockfile_path):
25
30
flags = portalocker .LOCK_EX | portalocker .LOCK_NB ,
26
31
** open_kwargs )
27
32
33
+ def try_to_create_lock_file (self ):
34
+ timeout = 5
35
+ check_interval = 0.25
36
+ current_time = getattr (time , "monotonic" , time .time )
37
+ timeout_end = current_time () + timeout
38
+ while timeout_end > current_time ():
39
+ try :
40
+ with open (self ._lockpath , 'x' ):
41
+ return True
42
+ except ValueError : # This needs to be the first clause, for Python 2 to hit it
43
+ logger .warning ("Python 2 does not support atomic creation of file" )
44
+ return False
45
+ except FileExistsError : # Only Python 3 will reach this clause
46
+ logger .warning ("Lock file exists, trying again after some time" )
47
+ time .sleep (check_interval )
48
+ return False
49
+
28
50
def __enter__ (self ):
51
+ if not self .try_to_create_lock_file ():
52
+ logger .warning ("Failed to create lock file" )
29
53
file_handle = self ._lock .__enter__ ()
30
54
file_handle .write ('{} {}' .format (os .getpid (), sys .argv [0 ]).encode ('utf-8' ))
31
55
return file_handle
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ def test_lock_for_high_workload(temp_location):
67
67
68
68
69
69
def test_lock_for_timeout (temp_location ):
70
- num_of_processes = 10
70
+ num_of_processes = 30
71
71
sleep_interval = 1
72
72
_run_multiple_processes (num_of_processes , temp_location , sleep_interval )
73
73
count = _validate_result_in_cache (temp_location )
You can’t perform that action at this time.
0 commit comments