autoload_replace=False,
)
- _CONFIGURE_MUTEX.acquire()
- try:
+ with _CONFIGURE_MUTEX:
table_to_map_config = dict(
(m.local_table, m)
for m in _DeferredMapperConfig.classes_for_base(
for map_config in _DeferredMapperConfig.classes_for_base(cls):
map_config.map()
- finally:
- _CONFIGURE_MUTEX.release()
+
_sa_decl_prepare = True
"""Indicate that the mapping of classes should be deferred.
self._scan_attributes()
- mapperlib._CONFIGURE_MUTEX.acquire()
- try:
+ with mapperlib._CONFIGURE_MUTEX:
clsregistry.add_class(self.classname, self.cls)
self._extract_mappable_attributes()
self._setup_inheritance()
self._early_mapping()
- finally:
- mapperlib._CONFIGURE_MUTEX.release()
def _early_mapping(self):
self.map()
upon a fixed set of classes.
"""
- mapperlib._CONFIGURE_MUTEX.acquire()
- try:
- while _mapper_registry:
- try:
- # can't even reliably call list(weakdict) in jython
- mapper, b = _mapper_registry.popitem()
- mapper.dispose()
- except KeyError:
- pass
- finally:
- mapperlib._CONFIGURE_MUTEX.release()
+ with mapperlib._CONFIGURE_MUTEX, _mapper_registry:
+ try:
+ # can't even reliably call list(weakdict) in jython
+ mapper, b = _mapper_registry.popitem()
+ mapper.dispose()
+ except KeyError:
+ pass
+
joinedload = strategy_options.joinedload._unbound_fn
# prevent this mapper from being constructed
# while a configure_mappers() is occurring (and defer a
# configure_mappers() until construction succeeds)
- _CONFIGURE_MUTEX.acquire()
- try:
+ with _CONFIGURE_MUTEX:
self.dispatch._events._new_mapper_instance(class_, self)
self._configure_inheritance()
self._configure_class_instrumentation()
Mapper._new_mappers = True
self._log("constructed")
self._expire_memoizations()
- finally:
- _CONFIGURE_MUTEX.release()
# major attributes initialized at the classlevel so that
# they can be Sphinx-documented.
if not Mapper._new_mappers:
return
- _CONFIGURE_MUTEX.acquire()
- try:
+ with _CONFIGURE_MUTEX:
global _already_compiling
if _already_compiling:
return
Mapper._new_mappers = False
finally:
_already_compiling = False
- finally:
- _CONFIGURE_MUTEX.release()
Mapper.dispatch._for_class(Mapper).after_configured()
try:
return self.pools[key]
except KeyError:
- self._create_pool_mutex.acquire()
- try:
+ with self._create_pool_mutex:
if key not in self.pools:
kw.pop("sa_pool_key", None)
pool = self.poolclass(
return pool
else:
return self.pools[key]
- finally:
- self._create_pool_mutex.release()
def connect(self, *args, **kw):
"""Activate a connection to the database.
return self.capacity + self.capacity * self.threshold
def _manage_size(self):
- if not self._mutex.acquire(False):
+ if self._mutex.locked():
return
try:
size_alert = bool(self.size_alert)
# avoid the 2to3 "next" transformation...
def _next():
- lock.acquire()
- try:
+ with lock:
return next(counter)
- finally:
- lock.release()
return _next
_lock = compat.threading.Lock()
def __new__(cls, name, doc=None, canonical=None):
- cls._lock.acquire()
- try:
+ with cls._lock:
sym = cls.symbols.get(name)
if sym is None:
cls.symbols[name] = sym = _symbol(name, doc, canonical)
return sym
- finally:
- symbol._lock.release()
@classmethod
def parse_user_argument(
condition.
"""
-
+import functools
from collections import deque
from time import time as _time
pass
+def atomic(f):
+ @functools.wraps(f)
+ def decorated(self, *args, **kwargs):
+ with self.mutex:
+ return f(self, *args, **kwargs)
+
+ return decorated
+
+
class Queue:
def __init__(self, maxsize=0, use_lifo=False):
"""Initialize a queue object with a given maximum size.
# If this queue uses LIFO or FIFO
self.use_lifo = use_lifo
+ @atomic
def qsize(self):
"""Return the approximate size of the queue (not reliable!)."""
- self.mutex.acquire()
- n = self._qsize()
- self.mutex.release()
- return n
+ return self._qsize()
+ @atomic
def empty(self):
"""Return True if the queue is empty, False otherwise (not
reliable!)."""
- self.mutex.acquire()
- n = self._empty()
- self.mutex.release()
- return n
+ return self._empty()
+ @atomic
def full(self):
"""Return True if the queue is full, False otherwise (not
reliable!)."""
- self.mutex.acquire()
- n = self._full()
- self.mutex.release()
- return n
+ return self._full()
def put(self, item, block=True, timeout=None):
"""Put an item into the queue.
(`timeout` is ignored in that case).
"""
- self.not_full.acquire()
- try:
+ with self.not_full:
if not block:
if self._full():
raise Full
self.not_full.wait(remaining)
self._put(item)
self.not_empty.notify()
- finally:
- self.not_full.release()
def put_nowait(self, item):
"""Put an item into the queue without blocking.
return an item if one is immediately available, else raise the
``Empty`` exception (`timeout` is ignored in that case).
"""
- self.not_empty.acquire()
- try:
+ with self.not_empty:
if not block:
if self._empty():
raise Empty
item = self._get()
self.not_full.notify()
return item
- finally:
- self.not_empty.release()
def get_nowait(self):
"""Remove and return an item from the queue without blocking.
# Check whether the queue is full
def _full(self):
- return self.maxsize > 0 and len(self.queue) == self.maxsize
+ return 0 < self.maxsize == len(self.queue)
# Put a new item in the queue
def _put(self, item):
dbapi = MockDBAPI()
def creator():
- mutex.acquire()
- try:
+ with mutex:
return dbapi.connect()
- finally:
- mutex.release()
success = []
for timeout in (None, 30):