-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_locker.py
37 lines (32 loc) · 1.17 KB
/
thread_locker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from threading import Lock
import server
class File_Thread_Locker:
"""
Handles Locking threads when interacting with specific files and directories
to prevent race conditions.
"""
def __init__(self, abspath):
"""
Initializes Thread Locker context handler.
Args:
abspath (str): Absolute path of file being interacted with.
"""
self.__abspath = abspath
def __enter__(self):
"""
Acquires lock from global thread locker dictionary and indicates use.
"""
if self.__abspath in server.thread_locks:
server.thread_locks[self.__abspath][0] += 1
server.thread_locks[self.__abspath][1].acquire(timeout=60)
else:
server.thread_locks[self.__abspath] = [1, Lock()]
server.thread_locks[self.__abspath][1].acquire()
def __exit__(self, exc_type, exc_value, trace):
"""
Releases lock from global thread locker and marks as not in use.
"""
status = server.thread_locks[self.__abspath]
status[1].release()
status[0] -= 1
server.thread_locks[self.__abspath] = status