本文整理汇总了Python中threading.Condition.wait方法的典型用法代码示例。如果您正苦于以下问题:Python Condition.wait方法的具体用法?Python Condition.wait怎么用?Python Condition.wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类threading.Condition
的用法示例。
在下文中一共展示了Condition.wait方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wait_statement
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
def wait_statement(self, wtime, N=5):
'''
'''
start_time = time.time()
if self.debug:
time.sleep(1)
else:
if isinstance(wtime, str):
wtime = float(self._get_interpolation_value(wtime))
if wtime > N:
c = Condition()
c.acquire()
wd = WaitDialog(wtime=wtime,
condition=c,
parent=self)
do_later(wd.edit_traits, kind='livemodal')
c.wait()
c.release()
do_later(wd.close)
else:
time.sleep(wtime)
msg = 'actual wait time %0.3f s' % (time.time() - start_time)
self.log_statement(msg)
示例2: Event
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class Event(object):
def __init__(self,):
self.__cond = Condition(Lock())
self.__flag = False
def isSet(self):
return self.__flag
is_set = isSet
def set(self):
self.__cond.acquire()
try:
self.__flag = True
self.__cond.notify_all()
finally:
self.__cond.release()
def clear(self):
self.__cond.acquire()
try:
self.__flag = False
finally:
self.__cond.release()
def wait(self, timeout=None):
self.__cond.acquire()
try:
if not self.__flag:
self.__cond.wait(timeout)
return self.__flag
finally:
self.__cond.release()
示例3: SimpleBlockingQueue
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class SimpleBlockingQueue(object):
def __init__(self):
self.items = deque()
self.condition = Condition()
self.interrupted = False
def put(self, item):
with self.condition:
self.items.pushleft(item)
def take(self, block=False):
with self.condition:
if block:
while not self.items:
self.condition.wait()
if self.interrupted:
self.interrupted = False
return None
elif not self.items:
return None
return self.items.popright()
def interrupt(self):
with self.condition:
self.interrupted = True
self.condition.notifyAll()
示例4: Node
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class Node(object):
STATE_INIT = 0
STATE_STARTING = 1
STATE_RUNNING = 2
STATE_STOPPED = 3
STATE_PARTITIONED = 4
STATE_FAILED = 5
state_str = {STATE_INIT: "Initial",
STATE_STARTING: "Starting",
STATE_RUNNING: "Running",
STATE_STOPPED: "Stopped",
STATE_FAILED: "Failed"}
def __init__(self, node_id):
self.node_id = node_id
self.state = Node.STATE_INIT
self.cv_lock = Lock()
self.cv = Condition(self.cv_lock)
def wait_for_state(self, state):
self.cv.acquire()
while self.state not in (state, Node.STATE_FAILED):
self.cv.wait()
self.cv.release()
if self.state != state:
raise ChistributedException("Node entered failed state while waiting for state %s" % Node.state_str[state])
def set_state(self, state):
self.cv.acquire()
self.state = state
self.cv.notify_all()
self.cv.release()
示例5: process_sequence
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
def process_sequence(self, sequence):
"""
@param sequence: the movement sequence to execute
@return: dictionary of sensor readings
"""
self.log.debug('process sequence')
# parse the json message
jseq = json.loads(sequence)
moveseq = jseq['sequence']
for moves in moveseq:
self.log.debug('processing move %s' % moves)
condition = Condition()
thread_count = 0
for move in moves:
thread_count += 1
servo_instruction = self.process_move(move)
move_servo_thread = ServoThread(servo_instruction, self.pwm_queue, condition, thread_count, self.update_current_pulse)
move_servo_thread.setDaemon(False)
move_servo_thread.setName('Servo %d' % servo_instruction['channel'])
move_servo_thread.start()
condition.acquire()
if thread_count > 0:
condition.wait()
# wait for all threads to finish before doing next loop
condition.release()
示例6: MessageQueue
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class MessageQueue(object):
"""The message queue used internally by Gossip."""
def __init__(self):
self._queue = deque()
self._condition = Condition()
def pop(self):
self._condition.acquire()
try:
while len(self._queue) < 1:
self._condition.wait()
return self._queue.pop()
finally:
self._condition.release()
def __len__(self):
return len(self._queue)
def __deepcopy__(self, memo):
newmq = MessageQueue()
newmq._queue = copy.deepcopy(self._queue, memo)
return newmq
def appendleft(self, msg):
self._condition.acquire()
try:
self._queue.appendleft(msg)
self._condition.notify()
finally:
self._condition.release()
示例7: SendBuffer
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class SendBuffer(object):
def __init__(self, max_size):
self.size = 0
self.frontbuffer = ''
self.full = Condition()
self.backbuffer = StringIO()
self.max_size = max_size
def __len__(self):
return len(self.frontbuffer) + self.size
def write(self, data):
dlen = len(data)
with self.full:
while self.size + dlen > self.max_size and dlen < self.max_size:
# if a single write is bigger than the buffer, swallow hard.
# No amount of waitng will make it fit.
self.full.wait()
self.backbuffer.write(data)
self.size += dlen
def to_sock(self, sock):
if not self.frontbuffer:
with self.full:
buf, self.backbuffer = self.backbuffer, StringIO()
self.size = 0
self.full.notify_all()
self.frontbuffer = buf.getvalue()
written = sock.send(self.frontbuffer)
self.frontbuffer = self.frontbuffer[written:]
示例8: request
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
def request(self, request_type, request, sequential=False):
"""
"""
cond = Condition()
# dict is used as reference type to allow result to be returned from
# seperate thread
response_cont = {}
def on_success(response_type, response):
response_cont.update({
"success": True,
"type": response_type,
"response": response,
})
with cond:
cond.notify()
def on_error(exception):
response_cont.update({
"success": False,
"exception": exception,
})
with cond:
cond.notify()
with cond:
self.request_async(request_type, request,
on_success, on_error,
sequential)
cond.wait()
if not response_cont["success"]:
raise response_cont["exception"]
return response_cont["type"], response_cont["response"]
示例9: RepceJob
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class RepceJob(object):
"""class representing message status we can use
for waiting on reply"""
def __init__(self, cbk):
"""
- .rid: (process-wise) unique id
- .cbk: what we do upon receiving reply
"""
self.rid = (os.getpid(), thread.get_ident(), time.time())
self.cbk = cbk
self.lever = Condition()
self.done = False
def __repr__(self):
return ':'.join([str(x) for x in self.rid])
def wait(self):
self.lever.acquire()
if not self.done:
self.lever.wait()
self.lever.release()
return self.result
def wakeup(self, data):
self.result = data
self.lever.acquire()
self.done = True
self.lever.notify()
self.lever.release()
示例10: Queue
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class Queue(object):
def __init__(self):
self.lock = Lock()
self.cond = Condition(self.lock)
self.data = []
self.live = True
def put(self, values):
self.cond.acquire()
self.data.extend(values)
self.cond.notify()
self.cond.release()
def get(self):
if not self.cond.acquire(False):
return []
self.cond.wait()
result = self.data
self.data = []
self.cond.release()
return result
def close(self):
self.cond.acquire()
self.live = False
self.cond.notify()
self.cond.release()
示例11: __init__
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class AsyncChannel:
# yes, i believe this is just Queue ... i was new to python and couldn't find it
def __init__ (self, buffer_size=1):
self.buffer = []
self.max_items = buffer_size
self.lock = Lock ()
self.not_empty = Condition (self.lock)
self.not_full = Condition (self.lock)
def get (self):
self.lock.acquire ()
while len (self.buffer) == 0:
self.not_empty.wait ()
val = self.buffer.pop (0)
self.not_full.notifyAll ()
self.lock.release ()
return val
def put (self, val):
self.lock.acquire ()
while len (self.buffer) == self.max_items:
self.not_full.wait ()
self.buffer.append (val)
self.not_empty.notifyAll ()
self.lock.release ()
示例12: LockedDirectories
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class LockedDirectories(object):
"""
Class that keeps a list of locked directories
"""
def __init__(self):
self.dirs = set()
self.cv = Condition()
def run_in(self, dir_):
"""
Start running in the directory and lock it
"""
self.cv.acquire()
while dir_ in self.dirs:
self.cv.wait()
self.dirs.add(dir_)
self.cv.release()
def done(self, dir_):
"""
Finished with the directory, unlock it
"""
self.cv.acquire()
self.dirs.remove(dir_)
self.cv.notify_all()
self.cv.release()
示例13: __init__
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class MockSock:
def __init__(self):
self.sent = six.binary_type()
self.send_q = deque()
self.recv_q = deque()
self.closed = False
self.lock = Lock()
self.cond = Condition(self.lock)
self.timeout = 0.5
def gettimeout(self):
return self.timeout
def close(self):
self.lock.acquire()
self.closed = True
self.lock.release()
def queue_send(self, num_bytes):
self.lock.acquire()
try:
self.send_q.append(num_bytes)
self.cond.notify_all()
finally:
self.lock.release()
def send(self, message):
self.lock.acquire()
try:
while not len(self.send_q):
self.cond.wait()
num_bytes = self.send_q.popleft()
if num_bytes < len(message):
self.send_q.appendleft(len(message) - num_bytes)
sent = min(num_bytes, len(message))
self.sent += message[:sent]
return sent
finally:
self.lock.release()
def queue_recv(self, message):
self.lock.acquire()
try:
self.recv_q.append(message)
self.cond.notify_all()
finally:
self.lock.release()
def recv(self, max_len):
self.lock.acquire()
try:
while not len(self.recv_q):
self.cond.wait()
message = self.recv_q.popleft()
recd = min(max_len, len(message))
if (recd < len(message)):
self.recv_q.appendleft(message[recd:])
return message[:recd]
finally:
self.lock.release()
示例14: Broadcaster
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class Broadcaster(object):
def __init__(self):
self._condition = Condition()
self._last_id = 0
self._queue = []
@acquire_then_notify
def send(self, item):
print "Sending message", item
self._last_id += 1
self._queue.append((self._last_id, item))
def _find_items(self, since_id=None):
if not since_id:
found = self._queue
else:
found = [(id, item) for (id,item) in self._queue if id > since_id]
return found
@acquire
def recv(self, since_id=None, timeout=10):
end_time = time() + timeout
while time() < end_time:
found = self._find_items(since_id)
if found:
break
print "Waiting"
self._condition.wait(timeout)
print "found %d" % len(found)
return found
示例15: Future
# 需要导入模块: from threading import Condition [as 别名]
# 或者: from threading.Condition import wait [as 别名]
class Future(object):
def __init__(self, func, *args, **kwargs):
self.__done = False
self.__result = None
self.__cond = Condition()
self.__func = func
self.__args = args
self.__kwargs = kwargs
self.__except = None
def __call__(self):
with self.__cond:
try:
self.__result = self.__func(*self.__args, **self.__kwargs)
except:
self.__result = None
self.__except = sys.exc_info()
self.__done = True
self.__cond.notify()
def result(self):
with self.__cond:
while not self.__done:
self.__cond.wait()
if self.__except:
exc = self.__except
reraise(exc[0], exc[1], exc[2])
result = self.__result
return copy.deepcopy(result)