本文整理汇总了Python中threading.Lock.aquire方法的典型用法代码示例。如果您正苦于以下问题:Python Lock.aquire方法的具体用法?Python Lock.aquire怎么用?Python Lock.aquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Lock
的用法示例。
在下文中一共展示了Lock.aquire方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BrainStudioBEClass
# 需要导入模块: from threading import Lock [as 别名]
# 或者: from threading.Lock import aquire [as 别名]
#.........这里部分代码省略.........
self.paused = True
# MUTEX HERE
self.block_for_client.release()
def inform_clients_terminated(self, master):
# MUTEX HERE
self.block_for_client.acquire()
for c in self.clients:
if c[1] != master:
c[0].sendall("terminated")
# MUTEX HERE
self.block_for_client.release()
def get_master(self, client_id):
while True:
# MUTEX HERE
self.block_for_local.acquire()
if self.steping_sim == False and (self.master == __master_not_set__ or client_id == __exiting__):
old_master = self.master
self.master = client_id
self.block_for_client.acquire()
self.paused = True
self.block_for_client.release()
# MUTEX HERE
self.block_for_local.release()
if client_id == __exiting__ and old_master > 0:
# MUTEX HERE
self.block_for_client.aquire()
# inform old client
for c in self.clients:
if c[2] == old_master:
c[0].sendall("slave")
break
# MUTEX HERE
self.block_for_client.release()
return True
# MUTEX HERE
self.block_for_local.release()
return False
def delete_client(self, client_id, client_address):
self.block_for_client.acquire()
# remove from client list
count = 0
for c in self.clients:
if c[2] == client_id:
del self.clients[count]
break
count += 1
if self.master == client_id:
self.master = __master_not_set__
self.paused = False
示例2: Event
# 需要导入模块: from threading import Lock [as 别名]
# 或者: from threading.Lock import aquire [as 别名]
class Event(newgpio.GPIO):
def __init__(self):
# PIN: [thread, bool ,gpioqueue, mode, HaveAppend, retourcallback]
# le callback est une lambda
self.lockSpi = Lock()
self.PinEvent = {}
# CHANNEL: [thread, bool , Function ,gpioqueue, [HaveAppend, value], retourcallback]
self.SpiEvent = {}
def __str__(self):
# String representation of all the thread active both Pin and Spi in the object
msg = [
"Pin # {0} mode : {1}\nCallback : {2}".format(k, v[3], " ".join([str(i) for i in v[2]]))
for k, v in self.PinEvent.items()
]
msg2 = [
"Channel # {0} function : {1}\nCallback : {2}".format(k, v[2], " ".join([str(i) for i in v[2]]))
for k, v in self.SpiEvent.items()
]
return "\n".join(msg + msg2)
def _threadEvent(self, pin=None, mode=0, endvalue=0):
egde = self._edge(mode)
with open("/sys/class/gpio/gpio%i/value" % pin, "r") as f:
previous_state = f.read()
po = select.poll()
po.register(f, select.POLLPRI)
while self.PinEvent[pin][1]:
f.seek(0)
state = f.read()
if edge != 3 and state[0] == edge:
if endvalue == 0:
# fin de la thread
self.PinEvent[pin][1] = False
keepo = False
else:
keepo = True
self.PinEvent[pin][2].dequeue(keep=keepo)
elif edge == 3 and state[0] != previous_state:
if endvalue == 0:
self.PinEvent[pin][1] = False
keepo = False
else:
keepo = True
self.PinEvent[pin][2].dequeue(keep=keepo)
previous_state = state[0]
def _threadSpi(self, channel, function, callback, value, endvalue):
while self.SpiEvent[channel][1]:
self.lockSpi.aquire()
try:
retour = function(channel)
finally:
self.lockSpi.release()
if retour > value:
# Condition de la thread est la do le shit
# Change la valeur pour etre appeler dans Event_Detect_Spi
self.SpiEvent[channel][4][0] = True
self.SpiEvent[channel][4][1] = retour
if endvalue == 0:
# finit la thread
self.SpiEvent[channel][1] = False
keepo = False
else:
keepo = True
self.SpiEvent[channel][3].dequeue(keep=keepo)
def Add_Event(self, pin, mode, callback):
# Add a event on a mode like falling to add a event 1 -> 0 and rising form 0 -> 1 or both on a pin
if not self.PinEvent.__contains__(pin):
queue = gpioqueue.queue(callback)
self.PinEvent[pin] = [Thread(target=self._threadEvent, args=(pin, mode)), True, queue, mode]
self.PinEvent[pin][0].start()
def Add_Event_Spi(self, channel, function, value, callback, endvalue):
if not self.SpiEvent.__contains__(channel):
queue = gpioqueue.queue(callback)
self.SpiEvent[channel] = [
Thread(
target=self._threadSpi,
args=(channel, function, queue, value, endvalue),
name="Thread channel {0}".format(channel),
),
callback,
[False, 0],
]
self.SpiEvent[channel][0].start()
return True
else:
return False
def Delete_Event(self, pin=None):
if pin is not None and self.PinEvent.__contains__(pin):
self.PinEvent[pin][1] = False
del self.PinEvent[pin]
return True
return False
def Delete_Event_Spi(self, channel):
if channel is not None and self.SpiEvent.__contains__(channel):
#.........这里部分代码省略.........