本文整理汇总了Python中threading.Lock类的典型用法代码示例。如果您正苦于以下问题:Python Lock类的具体用法?Python Lock怎么用?Python Lock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Lock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TempDirs
class TempDirs(object):
"""Tempdir manager."""
def __init__(self, tmpdir, prefix="rez_"):
self.tmpdir = tmpdir
self.prefix = prefix
self.dirs = set()
self.lock = Lock()
# previous version overloaded TempDirs.__del__ in an unsafe manner;
# the __del__ method is not guaranteed to be called before sys.modules begins
# breaking down, so the os.path call was failing with a muted AttributeError,
# leaving the directory on disk even when the program exited normally; by registering
# an atexit callback we should ensure the directories are cleared at shutdown
atexit.register(self.clear)
def mkdtemp(self, cleanup=True):
path = mkdtemp(dir=self.tmpdir, prefix=self.prefix)
if not cleanup:
return path
try:
self.lock.acquire()
self.dirs.add(path)
finally:
self.lock.release()
return path
def clear(self):
dirs = self.dirs
for path in dirs:
if os.path.exists(path):
shutil.rmtree(path)
示例2: __init__
class ProgressBarLogger:
def __init__(self, msg, total):
self.msg = msg
self.total = total
self.status = 0
self.lock = Lock()
def log(self, *_):
self.lock.acquire()
self.status += 1
self._print_progress_bar(self.status, self.total, prefix=self.msg, bar_length=50)
self.lock.release()
# from here http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console
# Print iterations progress
@staticmethod
def _print_progress_bar(iteration, total, prefix='', suffix='', decimals=2, bar_length=100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : number of decimals in percent complete (Int)
bar_length - Optional : character length of bar (Int)
"""
filled_length = int(round(bar_length * iteration / float(total)))
percents = round(100.00 * (iteration / float(total)), decimals)
bar_char = '#' * filled_length + '-' * (bar_length - filled_length)
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar_char, percents, '%', suffix))
sys.stdout.flush()
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()
示例3: _CoreScheduleThread
class _CoreScheduleThread(Thread):
def __init__(self,threadpool):
self.scheduletasks = [];
self.tasklock = Lock();
self.condition = Condition(Lock())
self.threadpool = threadpool
Thread.__init__(self)
def run(self):
while True:
self.condition.acquire()
if len(self.scheduletasks) == 0:
self.condition.wait();
else:
task = self.scheduletasks.pop(0)
if dates.current_timestamps()>=task.nexttime:
self.threadpool.execute(task.function,*task.args,**task.kwargs)
task.nexttime = dates.current_timestamps()+task.period;
else:
self.condition.wait(task.nexttime-dates.current_timestamps())
self.addtask(task)
self.condition.release()
def addtask(self,task): # copy on write
self.tasklock.acquire()
tasks = [ t for t in self.scheduletasks ]
tasks.append(task)
tasks.sort(key=lambda task:task.nexttime)
self.scheduletasks = tasks
self.tasklock.release()
示例4: __init__
def __init__(self,
component_builder=None,
project=None,
project_dir=None,
remote_storage=None,
fingerprint=None,
pull_models=None):
self._component_builder = component_builder
self._models = {}
self.status = STATUS_READY
self.current_training_processes = 0
self._reader_lock = Lock()
self._loader_lock = Lock()
self._writer_lock = Lock()
self._readers_count = 0
self._path = None
self._project = project
self.remote_storage = remote_storage
self.fingerprint = fingerprint
self.pull_models = pull_models
self.error_message = None
if project and project_dir:
self._path = os.path.join(project_dir, project)
self._search_for_models()
示例5: InMemoryItemValue
class InMemoryItemValue(object):
_lock = None
""":type _lock Lock"""
def __init__(self, value=None, expire_in=None):
self._lock = Lock()
self._value = value
self._expire_in = None
self._expire_in_time = None
self.update_expire_time(expire_in)
@property
def value(self):
return self._value
@value.setter
def value(self, val):
self._lock.acquire()
self._value = val
self._expire_in = datetime.now() + timedelta(seconds=float(self._expire_in_time)) if self._expire_in_time else None
self._lock.release()
def update_expire_time(self, t):
self._expire_in_time = t
@property
def is_expired(self):
return (self._expire_in - datetime.now()).days < 0 if self._expire_in else False
示例6: __init__
def __init__(self, item_number, person_capacity):
self.items_remaining_lock = Lock()
self.space_available_lock = Lock()
self.item_number = item_number
self.person_capacity = person_capacity
self.items_remaining = self.item_number
self.space_available = self.person_capacity
示例7: __init__
class Queue:
"""Command queue class
"""
def __init__(self):
self.lock = Lock()
self.locks = {}
def queue(self, command, *args):
check = inspect.getargspec(command)
cmdname = command.__name__ if command.__name__ else "uknown_cmd"
if len(check[0]) != len(args):
logging.warn("Queue command '%s' expected %u args, got %u!" % (cmdname, len(check[0]), len(args)))
# If we have enough args, try running the command
if len(args) >= len(check[0]):
args = args[:len(check[0])] # Resize arg list if needed
ret = None
server = args[0]
self.lock.acquire()
if not server in self.locks:
self.locks[server] = Lock()
self.lock.release()
self.locks[server].acquire()
# Run in an enclosure, so as to be able to release lock if it fails
try:
ret = command(*args)
except Exception as err:
logging.warn("Queue command returned error: %s" % err)
self.locks[server].release()
if ret:
return ret
return None
示例8: PandoraPool
class PandoraPool(object):
def __init__(self, poolSize, proxy=None, expireTime=3600):
self.size = poolSize
self.proxy = proxy
self.expire = expireTime
self.pool = [self.createPandoraAgent() for i in xrange(self.size)]
self.mutex = Lock()
def createPandoraAgent(self):
return PandoraAgent(datetime.now() + timedelta(0, self.expire), self.proxy)
def refreshPandoraAgent(self, agent):
if agent.isExpired():
agent.authenticate_connection()
agent.setExpireDate(datetime.now() + timedelta(0, self.expire))
return agent
def getAgent(self):
try:
return self.refreshPandoraAgent(self.pool.pop())
except IndexError:
return self.createPandoraAgent()
def hasAvailableConnections(self):
return len(self.pool) > 0
def releaseAgent(self, agent):
self.mutex.acquire()
if len(self.pool) < self.size:
self.pool.append(agent)
self.mutex.release()
示例9: __init__
def __init__(self, url, bucket, password="", verbose=False):
self.log = logger.logger("VBucketAwareMemcachedClient")
self.bucket = bucket
self.rest_username = bucket
self.rest_password = password
self._memcacheds = {}
self._vBucketMap = {}
self._vBucketMap_lock = Lock()
self._vBucketMapFastForward = {}
self._vBucketMapFastForward_lock = Lock()
#TODO: use regular expressions to parse the url
server = {}
if not bucket:
raise InvalidArgumentException("bucket can not be an empty string", parameters="bucket")
if not url:
raise InvalidArgumentException("url can not be an empty string", parameters="url")
if url.find("http://") != -1 and url.rfind(":") != -1 and url.find("/pools/default") != -1:
server["ip"] = url[url.find("http://") + len("http://"):url.rfind(":")]
server["port"] = url[url.rfind(":") + 1:url.find("/pools/default")]
server["username"] = self.rest_username
server["password"] = self.rest_password
self.servers = [server]
self.servers_lock = Lock()
self.rest = RestConnection(server)
self.reconfig_vbucket_map()
self.init_vbucket_connections()
self.dispatcher = CommandDispatcher(self)
self.dispatcher_thread = Thread(name="dispatcher-thread", target=self._start_dispatcher)
self.dispatcher_thread.daemon = True
self.dispatcher_thread.start()
self.streaming_thread = Thread(name="streaming", target=self._start_streaming, args=())
self.streaming_thread.daemon = True
self.streaming_thread.start()
self.verbose = verbose
示例10: DataWindow
class DataWindow(Thread):
def __init__(self,data_adapter):
Thread.__init__(self)
self.win = N.zeros((100,3))
self.winlock = Lock()
self.data_adapter = data_adapter
def run(self):
self.data_adapter.start()
self.running = True
while self.running:
self.winlock.acquire()
try:
while 1:
newdata = self.data_adapter.q.get(block=False)
self.win[:-1,:] = self.win[1:,:]
self.win[-1,:] = newdata[1:]
except Queue.Empty:
pass
finally:
self.winlock.release()
self.data_adapter.stop()
def stop(self):
self.running = False
示例11: __init__
class DebuggingLock:
def __init__(self, name):
self.lock = Lock()
self.name = name
def acquire(self, blocking = 1):
self.print_tb("Acquire lock")
self.lock.acquire(blocking)
self.logmsg("===== %s: Thread %s acquired lock\n"%
(self.name, currentThread().getName()))
def release(self):
self.print_tb("Release lock")
self.lock.release()
def logmsg(self, msg):
loglock.acquire()
logfile.write(msg + "\n")
logfile.flush()
loglock.release()
def print_tb(self, msg):
self.logmsg(".... %s: Thread %s attempting to %s\n"% \
(self.name, currentThread().getName(), msg) + \
"\n".join(traceback.format_list(traceback.extract_stack())))
示例12: run
def run(conn):
"""Function to handle running implosion generation in separate :py:class:`multithreading.Process`
:param conn: A connection, i.e. one end of a `Pipe()`
"""
# Need duck-checking instead of real type-checking...
assert hasattr(conn, 'send') and hasattr(conn, 'recv')
# Get the implosion object from the pipe:
imp = conn.recv()
assert isinstance(imp, Implosion)
connLock = Lock()
# Run in a separate thread in this process:
def impRun():
nonlocal imp, conn
try:
imp.generate()
except Exception as e:
connLock.acquire()
conn.send(e)
connLock.release()
t = Thread(target=impRun)
t.start()
while t.is_alive():
connLock.acquire()
conn.send(imp.progress())
connLock.release()
time.sleep(0.01)
# When the thread is done, send the Implosion object back:
conn.send(imp)
示例13: __init__
def __init__(self):
self.lock_mess=Lock()
self.lock_userlist = Lock()
self.users = []
self.message_list = []
super().__init__()
self.server = self.get_socket()
示例14: BatchInsertCollector
class BatchInsertCollector():
def __init__(self, cur, table_name, header=None, threshold=1000000):
if not isinstance(cur, MyCursor):
raise TypeError
self.cur = cur
self.table_name = table_name
print 'lyc here'
print table_name
if header is None:
print 'this way'
self.header = cur.get_header(table_name)
else:
print 'that way'
self.header = header
print 'I have a header'
self.sql_header = ''
self.cur_len = 0
self.reset_header()
self.threshold = threshold
self.values = []
self.stat_total = 0
self.mutex = Lock()
print 'initial finish'
def __del__(self):
self.flush()
self.cur.con.commit()
def reset_header(self):
self.sql_header = 'insert into %s (%s) values ' % (self.table_name, ','.join(self.header))
self.cur_len = len(self.sql_header)
def flush(self):
if len(self.values) == 0:
return
self.cur.cur.execute(self.sql_header + ','.join(self.values))
self.cur_len = len(self.sql_header)
self.cur.con.commit()
print 'flush called: %d records, total %d records' % (len(self.values), self.stat_total)
self.values = []
def append(self, data):
assert isinstance(data, DictItem)
self.mutex.acquire()
def find(val):
if val not in data.fields:
return u"''"
else:
return u"'%s'" % unicode(data[val])
cvalues = u','.join(map(find, self.header))
val1 = u"(%s)" % cvalues
# print self.cur_len
if self.cur_len + len(val1) > self.threshold:
self.flush()
self.values.append(val1)
self.cur_len += len(val1) + 1
self.stat_total += 1
self.mutex.release()
示例15: __init__
def __init__(self, service, channel, config = {}, _lazy = False):
# Close the connection while we set it up
self._closed = True
# Setup the config
self._config = deepcopy(DEFAULT_CONFIG)
self._config.update(config)
if self._config["connid"] is None:
self._config["connid"] = "conn{conn_id}".format(conn_id=self._connection_id_generator.__next__())
self._channel = channel
self._local_root = service(weakref.proxy(self)) # Why does this have to be weak
self._remote_root = None # This is set by root. Why??????
self._local_objects = Locking_dict() # {oid: native_obj} dictionary, orginal objects to object ids
self._proxy_cache = WeakValueDictionary() # {oid: proxy_obj} oid to proxy objects not owned by this connection
self._netref_classes_cache = {} # classes that have been??????
self._netref_proxy_builtin_cls = netref.PROXY_BUILTIN_TYPE_DICT # Already created in netref
self._seqcounter = itertools.count() # With this we will generate the msg seq numbers
self._recvlock = Lock()
self._sendlock = Lock()
self._sync_replies = {}
self._async_callbacks = {}
self._last_traceback = None
if not _lazy: # Should object automatically be set to go
self._init_service()
# Open this connection as we have finished setting it up
self._closed = False