-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
28 lines (22 loc) · 941 Bytes
/
utils.py
File metadata and controls
28 lines (22 loc) · 941 Bytes
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
from concurrent import futures
def thread_pool_wrapper(func):
def get_thread_pool_executor(*args, **kwargs) -> futures.Future:
if 'key' not in kwargs:
raise Exception('key is passed as named argument in current function call')
key = kwargs['key']
self = args[0]
key = hash(key) % len(self.thread_pool)
# submit is non-blocking and immediately returns future object
return self.thread_pool[key].submit(func, *args, **kwargs)
# to wait till all future are completed
# concurrent.futures.wait([future1, future2, future3])
return get_thread_pool_executor
def syncronized(func):
def wrapper(*args, **kwargs):
obj = args[0]
if not hasattr(obj, 'lock'):
raise Exception('lock attribute is missing in current class object')
lock = obj.lock
with lock:
return func(*args, **kwargs)
return wrapper