Ticket #3504: even_more_threadsafe_pool-r3762.diff
| File even_more_threadsafe_pool-r3762.diff, 2.7 KB (added by cboos, 6 years ago) |
|---|
-
trac/db/pool.py
34 34 to the pool. 35 35 """ 36 36 37 def __init__(self, pool, cnx ):37 def __init__(self, pool, cnx, tid): 38 38 ConnectionWrapper.__init__(self, cnx) 39 39 self._pool = pool 40 self._tid = tid 40 41 41 42 def close(self): 42 43 if self.cnx: 43 self._pool._return_cnx(self.cnx )44 self._pool._return_cnx(self.cnx, self._tid) 44 45 self.cnx = None 45 46 46 47 def __del__(self): … … 62 63 def get_cnx(self, timeout=None): 63 64 start = time.time() 64 65 self._available.acquire() 66 print `self._active` 65 67 try: 66 68 tid = threading._get_ident() 67 69 if tid in self._active: 68 self._active[tid][0] += 1 69 return PooledConnection(self, self._active[tid][1]) 70 num, cnx = self._active.get(tid) 71 if num == 0: 72 cnx.rollback() 73 self._active[tid][0] = num + 1 74 return PooledConnection(self, cnx, tid) 70 75 while True: 71 76 if self._dormant: 72 77 cnx = self._dormant.pop() … … 89 94 else: 90 95 self._available.wait() 91 96 self._active[tid] = [1, cnx] 92 return PooledConnection(self, cnx )97 return PooledConnection(self, cnx, tid) 93 98 finally: 94 99 self._available.release() 95 100 96 def _return_cnx(self, cnx ):101 def _return_cnx(self, cnx, tid): 97 102 self._available.acquire() 98 103 try: 99 tid = threading._get_ident()100 104 if tid in self._active: 101 105 num, cnx_ = self._active.get(tid) 102 106 assert cnx is cnx_ … … 111 115 # Note: self._available *must* be acquired 112 116 if tid in self._active: 113 117 cnx = self._active.pop(tid)[1] 114 if cnx not in self._dormant: 115 cnx.rollback()116 if cnx.poolable:118 if cnx not in self._dormant: # hm, how could that happen? 119 if cnx.poolable: # i.e. we can manipulate it from other threads 120 cnx.rollback() 117 121 self._dormant.append(cnx) 118 else: 122 elif tid == threading._get_ident(): 123 cnx.rollback() # non-poolable but same thread: close 119 124 cnx.close() 120 125 self._cursize -= 1 126 else: # non-poolable, different thread: do nothing 127 self._active[tid][0] = 0 121 128 self._available.notify() 122 129 123 130 def shutdown(self, tid=None):
