本文整理汇总了Python中threading.RLock类的典型用法代码示例。如果您正苦于以下问题:Python RLock类的具体用法?Python RLock怎么用?Python RLock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RLock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Framer
class Framer(Packer):
HEADER="!4s4B"
def __init__(self, sock):
self.sock = sock
self.sock_lock = RLock()
self.tx_buf = ""
self.rx_buf = ""
self.security_layer_tx = None
self.security_layer_rx = None
self.maxbufsize = 65535
def aborted(self):
return False
def write(self, buf):
self.tx_buf += buf
def flush(self):
self.sock_lock.acquire()
try:
if self.security_layer_tx:
try:
cipher_buf = self.security_layer_tx.encode(self.tx_buf)
except SASLError, e:
raise Closed(str(e))
self._write(cipher_buf)
else:
示例2: ThreadSafeFSM
class ThreadSafeFSM(InstrumentFSM):
"""
A FSM class that provides thread locking in on_event to
prevent simultaneous thread reentry.
"""
def __init__(self, states, events, enter_event, exit_event):
"""
"""
super(ThreadSafeFSM, self).__init__(states, events, enter_event, exit_event)
self._lock = RLock()
def on_event(self, event, *args, **kwargs):
"""
"""
self._lock.acquire(True)
ex = None
try:
result = super(ThreadSafeFSM, self).on_event(event, *args, **kwargs)
except Exception as ex:
result = None
log.error("Unhandled Exception")
log.exception(ex)
finally:
self._lock.release()
if ex:
raise ex
return result
示例3: SingleIndexCache
class SingleIndexCache(object):
def __init__(self):
self.lock = RLock()
self.cached_vals = []
def __enter__(self):
self.lock.acquire()
return self
def fill(self, values):
self.cached_vals = values
def wipe(self):
self.cached_vals = []
def get_index(self, real_funct, ind_name, **kwargs):
kwargs.setdefault("max_results", 999999)
if not self.cached_vals:
recvs = real_funct(ind_name, **kwargs).results
self.fill(recvs)
def filter(self, startkey, endkey, max_results=1):
c = self.cached_vals
for (curr_val, obj_key) in c:
if max_results == 0:
break
if curr_val >= startkey:
if curr_val <= endkey:
max_results -= 1
yield (curr_val, obj_key)
else:
break
def __exit__(self, *args, **kwargs):
self.lock.release()
示例4: XBeeTransparentListener
class XBeeTransparentListener(Thread):
def __init__(self, xbee_serial):
super().__init__()
self.xbser = xbee_serial
self.daemon = True
self.stopped = False
self.pause = RLock()
def run(self):
while not self.stopped and self.xbser.is_open:
with self.pause:
try:
line = self.xbser.readline()
if line:
print('>', line.strip())
except Exception as ex:
print(str(ex))
def stop(self):
self.stopped = True
def pause(self):
self.pause.acquire()
def unpause(self):
self.pause.release()
示例5: __init__
class SimpleCSVWriter:
def __init__(self, filename, fields=None):
self.filename = filename
self.lock = RLock()
self.isFirstRow = True
self.fields = fields
def writerow(self, d):
self.lock.acquire()
fields = self.fields if self.fields is not None else d.keys()
if self.isFirstRow:
# dump fields
f = open(self.filename , "w")
writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_ALL)
row = [k for k in fields]
writer.writerow(row)
f.close()
self.isFirstRow = False
# dump object
row = [d.get(k,'') for k in fields]
f = open(self.filename , "a")
writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_ALL)
writer.writerow(row)
f.close()
self.lock.release()
示例6: __init__
def __init__(self):
Thread.__init__(self)
self.positions = list()
self.stopped = False
self.startTime = time.time()
self.timer = 0
self.counter = 0
self.total_uavs = 0 # Total number of UAVs in the air
self.steps_counter = 0
self.turn_kill_counter = 0
self.writeState = Event()
self.writeCounter = 0
self.readState = Event()
self.readCounter = 0
self.lock = RLock()
self.lock2 = RLock()
try:
self.readState.set()
logging.info('Read state set.')
except:
logging.error('Read state not set.')
示例7: __init__
def __init__(self, req, proxy, logger, task, exit_check=None, ignored_errors=[]):
Thread.__init__(self, name = "monitor%s" % task.guid)
Thread.setDaemon(self, True)
# the count of votes per error code
self.vote_result = {}
# the error code to be ignored
self.vote_cleared = set().union(ignored_errors)
self.thread_last_seen = {}
self.dctlock = RLock()
self.votelock = RLock()
self.thread_ref = {}
self.thread_zombie = set()
# HttpReq instance
self.req = req
# proxy.Pool instance
self.proxy = proxy
self.logger = logger
self.task = task
self._exit = exit_check if exit_check else lambda x: False
self._cleaning_up = False
if os.name == "nt":
self.set_title = lambda s:os.system("TITLE %s" % (
s if PY3K else s.encode(CODEPAGE, 'replace')))
elif os.name == 'posix':
import sys
self.set_title = lambda s:sys.stdout.write("\033]2;%s\007" % (
s if PY3K else s.encode(CODEPAGE, 'replace')))
示例8: CacheDict
class CacheDict(dict):
_cacheTimeout = 0
_accesslock = None
def __init__(self, timeout):
self._cacheTimeout = timeout
self._accesslock = RLock()
def get(self, key):
return self[key]['value']
def cache(self, key, value):
self[key] = { 'time' : time.time(), 'value' : value }
def isObsolete(self, key):
return (not self.has_key(key) or
time.time() - self[key]['time'] > self._cacheTimeout)
def invalidate(self, key):
if self.has_key(key):
return self.pop(key)['value']
def acquire(self):
self._accesslock.acquire()
def release(self):
self._accesslock.release()
示例9: DataSignaler
class DataSignaler(object):
def __init__(self, name, pruneFunc, data):
super(DataSignaler, self).__init__()
assert isinstance(data,TreeFunctioned)
self.data = data
self.event_signaler = EventSignaler(key=name)
if pruneFunc is not None:
assert callable(pruneFunc)
self.prune_func = pruneFunc
self._lock = RLock()
def add(self, value):
self._lock.acquire()
try:
self.data.addToTreeByFunction(value)
finally:
self._lock.release()
data = {self.event_signaler.key : {'data': self.data}}
self.event_signaler.signalEvent(data)
def prune(self):
if self.prune_func is not None:
return criticalSection(self._lock, lambda: self.prune_func(dataStructure=self.data))
def inByFunction(self, value, hashFuncList=None, depth=0):
return criticalSection(self._lock, lambda: self.data.inByFunction(value, hashFuncList, depth))
def getOriginalByFunction(self, value, hashFuncList=None, depth=0):
return criticalSection(self._lock, lambda: self.data.getOriginalByFunction(value, hashFuncList, depth))
示例10: Database
class Database(object):
UNICODE_TRANSLATE = {ord(u'ö'): u'o', ord(u'ä'): u'a', ord(u'ü'): u'u'}
def __init__(self, filename):
self.database_file = filename
self.channel_map = {}
self._db = None
self.lock = RLock()
def close(self):
self.commit()
cur = self.cursor(lock=True)
log('Optimizing database for faster startup.', sender=self)
cur.execute("VACUUM")
cur.close()
self.lock.release()
self._db.close()
self._db = None
def log(self, message, *args, **kwargs):
if False:
try:
message = message % args
log('%s', message, sender=self)
except TypeError, e:
log('Exception in log(): %s: %s', e, message, sender=self)
示例11: __init__
def __init__(self, cache, store, logger, max_writer_threads=30):
'''Max. throughput for files < 1MB is max_writer_threads * 100KB per second.
:param max_writer_threads: Max. number of writer threads to use.
'''
self.logger = logger
self.stats = WorkerStats()
self.cache = cache
self.store = store
self.max_writer_threads = max_writer_threads
self.WRITE_TIMELIMIT = 60*60*2 #2h
self.lock = RLock()
self.protect_cache_from_write_access = RLock() #could also be normal lock
self.oldest_modified_date = {} #keep track of modified date of a cache entry when it is first enqueued for upload. Their contents might change during upload.
self.removers = []
self.writers = []
self.readers = []
self._stop = False
self.thread = None
self.last_reconnect = time.time()
self._heartbeat = time.time()
#used for waiting when quota errors occur
self.skip_starting_new_writers_for_next_x_cycles = 0
self.do_profiling = False
self.upload_process_pool = WriteWorkerProcesses(store, logger)
self.logger.info("initialized StoreSyncThread")
示例12: close_dynamic_queue
def close_dynamic_queue(self, dynamic_queue_name):
if self._disconnecting:
self.logger.info("Connection factory disconnecting, aborting close_dynamic_queue")
return
else:
self.logger.log(TRACE1, "close_dynamic_queue -> not disconnecting")
if not self._is_connected:
# If we're not connected then all dynamic queues had been already closed.
self.logger.log(TRACE1, "close_dynamic_queue -> _is_connected1 %s" % self._is_connected)
return
else:
self.logger.log(TRACE1, "close_dynamic_queue -> _is_connected2 %s" % self._is_connected)
lock = RLock()
lock.acquire()
try:
dynamic_queue = self._open_dynamic_queues_cache[dynamic_queue_name]
dynamic_queue.close()
self._open_dynamic_queues_cache.pop(dynamic_queue_name, None)
self._open_send_queues_cache.pop(dynamic_queue_name, None)
self._open_receive_queues_cache.pop(dynamic_queue_name, None)
self.logger.log(TRACE1, "Successfully closed a dynamic queue [%s]" % (
dynamic_queue_name))
finally:
lock.release()
示例13: open_dynamic_queue
def open_dynamic_queue(self):
if self._disconnecting:
self.logger.info("Connection factory disconnecting, aborting open_dynamic_queue")
return
else:
self.logger.log(TRACE1, "open_dynamic_queue -> not disconnecting")
if not self._is_connected:
self.logger.log(TRACE1, "open_dynamic_queue -> _is_connected1 %s" % self._is_connected)
self._connect()
self.logger.log(TRACE1, "open_dynamic_queue -> _is_connected2 %s" % self._is_connected)
dynamic_queue = self.mq.Queue(self.mgr, self.dynamic_queue_template,
self.CMQC.MQOO_INPUT_SHARED)
# A bit hackish, but there's no other way to get its name.
dynamic_queue_name = dynamic_queue._Queue__qDesc.ObjectName.strip()
lock = RLock()
lock.acquire()
try:
self._open_dynamic_queues_cache[dynamic_queue_name] = dynamic_queue
finally:
lock.release()
self.logger.log(TRACE1, "Successfully created a dynamic queue, descriptor [%s]" % (
dynamic_queue._Queue__qDesc))
return dynamic_queue_name
示例14: XBeeTransparentListener
class XBeeTransparentListener(Thread):
def __init__(self, on_received=None):
super().__init__()
self.xbser = None
self.on_received = on_received
self.daemon = True
self.stopped = False
self.pause = RLock()
def run(self):
while not self.stopped and self.xbser.is_open:
with self.pause:
try:
line = self.xbser.readline()
if line:
self.received(line)
except Exception as ex:
print(str(ex))
def received(self, line):
"""Subclasses may override this method, or provide a callback function when instance is created"""
if self.on_received:
self.on_received(line)
else:
print('[XBee]', line.strip())
def stop(self):
self.stopped = True
def pause(self):
self.pause.acquire()
def unpause(self):
self.pause.release()
示例15: VM_Pool
class VM_Pool(object):
def __init__(self, vm_map):
self.proc_mgr = proc_mgmt.ProcMgr()
self.vm_map = vm_map
self.vm_rdy = {}
self.init_map()
self.pool_gate = RLock()
def acquire(self, *names):
self.pool_gate.acquire()
for name in names:
if self.vm_rdy.get(name):
self.vm_rdy[name] = False
return self.vm_map.get(name)
self.pool_gate.release()
return None
def release(self, name):
self.vm_rdy[name] = True
def init_map(self):
for name, vm_obj in self.vm_map.items():
self.vm_rdy[name] = True
def __str__(self):
string = 'Pool:'
for vm in self.vm_map.keys():
string += vm + ": " + str(self.vm_rdy.get(vm)) + ", "
return string