本文整理汇总了Python中threading.Lock.__enter__方法的典型用法代码示例。如果您正苦于以下问题:Python Lock.__enter__方法的具体用法?Python Lock.__enter__怎么用?Python Lock.__enter__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Lock
的用法示例。
在下文中一共展示了Lock.__enter__方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Mutex
# 需要导入模块: from threading import Lock [as 别名]
# 或者: from threading.Lock import __enter__ [as 别名]
class Mutex(object):
"""Models a mutex object which provides a lock over an object,
identified by name.
:type name: str
:param name: the name of the lock to be created (must be unique)
"""
_current_mutex = {}
def __init__(self, name):
self._lock = Lock()
self.name = name
@classmethod
def get_mutex(cls, name="_default"):
"""Class method to create unique mutex, with name or using default
name "_default".
"""
if name not in Mutex._current_mutex:
Mutex._current_mutex[name] = Mutex(name)
return Mutex._current_mutex[name]
def __del__(self):
Lock.__del__(self)
if self.name in Mutex._current_mutex:
del Mutex._current_mutex[self.name]
def __enter__(self):
return self._lock.__enter__()
def __exit__(self, typ, value, traceback):
return self._lock.__exit__()
示例2: PickleableLock
# 需要导入模块: from threading import Lock [as 别名]
# 或者: from threading.Lock import __enter__ [as 别名]
class PickleableLock(object):
def __init__(self):
self.lock = Lock()
def __getstate__(self):
return ''
def __setstate__(self, value):
return self.__init__()
def __getattr__(self, item):
return self.lock.__getattr__(item)
def __enter__(self):
self.lock.__enter__()
def __exit__(self, exc_type, exc_val, exc_tb):
self.lock.__exit__(exc_type, exc_val, exc_tb)
示例3: PicklableLock
# 需要导入模块: from threading import Lock [as 别名]
# 或者: from threading.Lock import __enter__ [as 别名]
class PicklableLock(object):
""" A wrapper for threading.Lock which discards its state during pickling and
is reinitialized unlocked when unpickled.
"""
def __init__(self):
self.lock = Lock()
def __getstate__(self):
return ''
def __setstate__(self, value):
return self.__init__()
def __enter__(self):
self.lock.__enter__()
def __exit__(self, exc_type, exc_val, exc_tb):
self.lock.__exit__(exc_type, exc_val, exc_tb)
示例4: _LoggingMutexThreading
# 需要导入模块: from threading import Lock [as 别名]
# 或者: from threading.Lock import __enter__ [as 别名]
class _LoggingMutexThreading(_LoggingMutex):
def __init__(self, name):
super(_LoggingMutexThreading, self).__init__(name)
from threading import Lock
self.mutex = Lock()
def currentThreadID(self):
from threading import currentThread
return currentThread().ident
def _acquire(self):
self.mutex.acquire()
def _release(self):
self.mutex.release()
def enterMutex(self):
self.mutex.__enter__()
def exitMutex(self, *args, **kwargs):
self.mutex.__exit__(*args, **kwargs)
示例5: Mutex
# 需要导入模块: from threading import Lock [as 别名]
# 或者: from threading.Lock import __enter__ [as 别名]
class Mutex(object):
_current_mutex = {}
def __init__(self, name):
self._lock = Lock()
self.name = name
@classmethod
def get_mutex(cls, name="_default"):
if name not in Mutex._current_mutex:
Mutex._current_mutex[name] = Mutex(name)
return Mutex._current_mutex[name]
def __del__(self):
Lock.__del__(self)
if self.name in Mutex._current_mutex:
del Mutex._current_mutex[self.name]
def __enter__(self):
return self._lock.__enter__()
def __exit__(self, typ, value, traceback):
return self._lock.__exit__()
示例6: SerializableLock
# 需要导入模块: from threading import Lock [as 别名]
# 或者: from threading.Lock import __enter__ [as 别名]
class SerializableLock(object):
_locks = WeakValueDictionary()
""" A Serializable per-process Lock
This wraps a normal ``threading.Lock`` object and satisfies the same
interface. However, this lock can also be serialized and sent to different
processes. It will not block concurrent operations between processes (for
this you should look at ``multiprocessing.Lock`` or ``locket.lock_file``
but will consistently deserialize into the same lock.
So if we make a lock in one process::
lock = SerializableLock()
And then send it over to another process multiple times::
bytes = pickle.dumps(lock)
a = pickle.loads(bytes)
b = pickle.loads(bytes)
Then the deserialized objects will operate as though they were the same
lock, and collide as appropriate.
This is useful for consistently protecting resources on a per-process
level.
The creation of locks is itself not threadsafe.
"""
def __init__(self, token=None):
self.token = token or str(uuid.uuid4())
if self.token in SerializableLock._locks:
self.lock = SerializableLock._locks[self.token]
else:
self.lock = Lock()
SerializableLock._locks[self.token] = self.lock
def acquire(self, *args):
return self.lock.acquire(*args)
def release(self, *args):
return self.lock.release(*args)
def __enter__(self):
self.lock.__enter__()
def __exit__(self, *args):
self.lock.__exit__(*args)
@property
def locked(self):
return self.locked
def __getstate__(self):
return self.token
def __setstate__(self, token):
self.__init__(token)
def __str__(self):
return "<%s: %s>" % (self.__class__.__name__, self.token)
__repr__ = __str__