forked from treeverse/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.py
More file actions
59 lines (46 loc) 路 1.32 KB
/
Copy pathpool.py
File metadata and controls
59 lines (46 loc) 路 1.32 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import threading
from collections import deque
from contextlib import contextmanager
from funcy import memoize, wrap_with
@contextmanager
def get_connection(conn_func, *args, **kwargs):
pool = get_pool(conn_func, *args, **kwargs)
conn = pool.get_connection()
try:
yield conn
except Exception:
conn.close()
raise
else:
pool.release(conn)
@wrap_with(threading.Lock())
@memoize
def get_pool(conn_func, *args, **kwargs):
return Pool(conn_func, *args, **kwargs)
def close_pools():
for pool in get_pool.memory.values():
pool.close()
get_pool.memory.clear()
class Pool:
def __init__(self, conn_func, *conn_args, **conn_kwargs):
self._conn_func = conn_func
self._conn_args = conn_args
self._conn_kwargs = conn_kwargs
self._conns = deque()
self._closed = False
def __del__(self):
self.close()
def close(self):
while self._conns:
self._conns.pop().close()
self._closed = True
def get_connection(self):
try:
return self._conns.popleft()
except IndexError:
return self._conn_func(*self._conn_args, **self._conn_kwargs)
def release(self, conn):
if self._closed:
conn.close()
else:
self._conns.append(conn)