本文整理汇总了Python中tornado.locks.Condition类的典型用法代码示例。如果您正苦于以下问题:Python Condition类的具体用法?Python Condition怎么用?Python Condition使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Condition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ImageManager
class ImageManager():
def __init__(self):
# Image data
self._frame = None
# Flow control
self._condition = Condition()
def timestamp(self, img):
now = datetime.datetime.now()
stamp.stamp(img, (10, 10), str(now), size=20)
return img
def update_frame(self, frame):
self._frame = BytesIO(frame)
self.ready = True
@property
def frame(self):
return self._frame
@property
def ready(self):
return self._condition
@ready.setter
def ready(self, cond):
if cond is True:
self._condition.notify_all()
示例2: get_data
def get_data(cls, account, source_filter, limit=100, skip=0):
"""
Gathers card information from Google Sheets
GET https://spreadsheets.google.com/feeds/list/[spreadsheet]/[worksheet]/private/full
"""
if not account or not account.enabled:
raise ValueError('cannot gather information without an account')
client = AsyncHTTPClient()
if source_filter.spreadsheet is None:
raise ValueError('required parameter spreadsheet missing')
if source_filter.worksheet is None:
raise ValueError('required parameter worksheet missing')
uri = "https://docs.google.com/spreadsheets/d/{}/export?format=csv&gid={}".format(
source_filter.spreadsheet, source_filter.worksheet
)
app_log.info(
"Start retrieval of worksheet {}/{} for {}".format(source_filter.spreadsheet, source_filter.worksheet,
account._id))
lock = Condition()
oauth_client = account.get_client()
uri, headers, body = oauth_client.add_token(uri)
req = HTTPRequest(uri, headers=headers, body=body, streaming_callback=lambda c: cls.write(c))
client.fetch(req, callback=lambda r: lock.notify())
yield lock.wait(timeout=timedelta(seconds=MAXIMUM_REQ_TIME))
app_log.info(
"Finished retrieving worksheet for {}".format(account._id))
示例3: Window
class Window(object):
def __init__(self, parent, stream_id, initial_window_size):
self.parent = parent
self.stream_id = stream_id
self.cond = Condition()
self.closed = False
self.size = initial_window_size
def close(self):
self.closed = True
self.cond.notify_all()
def _raise_error(self, code, message):
if self.parent is None:
raise ConnectionError(code, message)
else:
raise StreamError(self.stream_id, code)
def adjust(self, amount):
self.size += amount
if self.size > constants.MAX_WINDOW_SIZE:
self._raise_error(constants.ErrorCode.FLOW_CONTROL_ERROR,
"flow control window too large")
self.cond.notify_all()
def apply_window_update(self, frame):
try:
window_update, = struct.unpack('>I', frame.data)
except struct.error:
raise ConnectionError(constants.ErrorCode.FRAME_SIZE_ERROR,
"WINDOW_UPDATE incorrect size")
# strip reserved bit
window_update = window_update & 0x7fffffff
if window_update == 0:
self._raise_error(constants.ErrorCode.PROTOCOL_ERROR,
"window update must not be zero")
self.adjust(window_update)
@gen.coroutine
def consume(self, amount):
while not self.closed and self.size <= 0:
yield self.cond.wait()
if self.closed:
raise StreamClosedError()
if self.size < amount:
amount = self.size
if self.parent is not None:
amount = yield self.parent.consume(amount)
self.size -= amount
raise gen.Return(amount)
示例4: clear
def clear(self):
"""Reset this PeerGroup.
This closes all connections to all known peers and forgets about
these peers.
:returns:
A Future that resolves with a value of None when the operation
has finished
"""
if self._resetting:
# If someone else is already resetting the PeerGroup, just block
# on them to be finished.
yield self._reset_condition.wait()
raise gen.Return(None)
self._resetting = True
if self._reset_condition is None:
self._reset_condition = Condition()
try:
for peer in self._peers.values():
peer.close()
finally:
self._peers = {}
self._resetting = False
self._reset_condition.notify_all()
示例5: __init__
def __init__ (self, device_server, stream, address):
self.fw_version = 0.0
self.recv_msg_cond = Condition()
self.recv_msg = {}
self.send_msg_sem = Semaphore(1)
self.pending_request_cnt = 0
self.device_server = device_server
self.stream = stream
self.address = address
self.stream.set_nodelay(True)
self.timeout_handler_onlinecheck = None
self.timeout_handler_offline = None
self.killed = False
self.sn = ""
self.private_key = ""
self.node_id = 0
self.iv = None
self.cipher_down = None
self.cipher_up = None
#self.state_waiters = []
#self.state_happened = []
self.event_waiters = []
self.event_happened = []
self.ota_ing = False
self.ota_notify_done_future = None
self.post_ota = False
self.online_status = True
示例6: __init__
def __init__(self, session, home):
self.scrapper = Scrapper(session, home)
self.session = session
self.home = home
self.condition = Condition()
tornado.ioloop.IOLoop.current()\
.spawn_callback(self.main)
示例7: __init__
def __init__(self, application, request, **kwargs):
super(PingHandler, self).__init__(application, request, **kwargs)
self.callback_queue = None
self.condition = Condition()
self.response = None
self.corr_id = str(uuid.uuid4())
self.in_channel = self.application.get_app_component().rabbitmq[
'client'].channels['in']
示例8: PingHandler
class PingHandler(firenado.tornadoweb.TornadoHandler):
def __init__(self, application, request, **kwargs):
super(PingHandler, self).__init__(application, request, **kwargs)
self.callback_queue = None
self.condition = Condition()
self.response = None
self.corr_id = str(uuid.uuid4())
self.in_channel = self.application.get_app_component().rabbitmq[
'client'].channels['in']
@gen.coroutine
def post(self):
self.in_channel.queue_declare(exclusive=True,
callback=self.on_request_queue_declared)
yield self.condition.wait()
self.write(self.response)
def on_request_queue_declared(self, response):
logger.info('Request temporary queue declared.')
self.callback_queue = response.method.queue
self.in_channel.basic_consume(self.on_response, no_ack=True,
queue=self.callback_queue)
self.in_channel.basic_publish(
exchange='',
routing_key='ping_rpc_queue',
properties=pika.BasicProperties(
reply_to=self.callback_queue,
correlation_id=self.corr_id,
),
body=self.request.body)
def on_response(self, ch, method, props, body):
if self.corr_id == props.correlation_id:
self.response = {
'data': body.decode("utf-8"),
'date': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
}
self.in_channel.queue_delete(queue=self.callback_queue)
self.condition.notify()
示例9: test_future_close_callback
def test_future_close_callback(self):
# Regression test for interaction between the Future read interfaces
# and IOStream._maybe_add_error_listener.
rs, ws = yield self.make_iostream_pair()
closed = [False]
cond = Condition()
def close_callback():
closed[0] = True
cond.notify()
rs.set_close_callback(close_callback)
try:
ws.write(b'a')
res = yield rs.read_bytes(1)
self.assertEqual(res, b'a')
self.assertFalse(closed[0])
ws.close()
yield cond.wait()
self.assertTrue(closed[0])
finally:
rs.close()
ws.close()
示例10: __init__
def __init__(self, buf=None, auto_close=True):
"""In-Memory based stream
:param buf: the buffer for the in memory stream
"""
self._stream = deque()
if buf:
self._stream.append(buf)
self.state = StreamState.init
self._condition = Condition()
self.auto_close = auto_close
self.exception = None
示例11: GameScrapperWorker
class GameScrapperWorker(object):
def __init__(self, session, home):
self.scrapper = Scrapper(session, home)
self.session = session
self.home = home
self.condition = Condition()
tornado.ioloop.IOLoop.current()\
.spawn_callback(self.main)
@tornado.gen.coroutine
def main(self):
_log().info('scrapper sleeping')
yield self.condition.wait()
_log().info('scrapper woke up')
self.scrapper.scrap_missing()
tornado.ioloop.IOLoop.current()\
.spawn_callback(self.main)
raise tornado.gen.Return(False)
示例12: __init__
def __init__(self, tchannel, score_threshold=None):
"""Initializes a new PeerGroup.
:param tchannel:
TChannel used for communication by this PeerGroup
:param score_threshold:
A value in the ``[0, 1]`` range. If specifiede, this requires that
chosen peers havea score higher than this value when performing
requests.
"""
self.tchannel = tchannel
self._score_threshold = score_threshold
# Dictionary from hostport to Peer.
self._peers = {}
# Notified when a reset is performed. This allows multiple coroutines
# to block on the same reset.
self._resetting = False
self._reset_condition = Condition()
示例13: ImporterWorker
class ImporterWorker(object):
def __init__(self, session, home, scrapper):
self.session = session
self.home = home
self.condition = Condition()
self.scrapper = scrapper
tornado.ioloop.IOLoop.current()\
.spawn_callback(self.main)
@tornado.gen.coroutine
def main(self):
_log().info('importer sleeping')
yield self.condition.wait()
_log().info('importer woke up')
changed = True
while changed:
changed = yield self.work()
tornado.ioloop.IOLoop.current()\
.spawn_callback(self.main)
@tornado.gen.coroutine
def work(self):
changed = False
fi = slickbird.FileImporter(self.session, self.home)
for f in self.session.query(orm.Importerfile)\
.filter(orm.Importerfile.status == 'scanning'):
changed = True
r, status = fi.file_import(f.filename)
f.status = status
if status == 'moved':
self.scrapper.condition.notify()
yield tornado.gen.moment
self.session.commit()
self.scrapper.condition.notify()
raise tornado.gen.Return(changed)
示例14: __init__
def __init__(self):
# Image data
self._frame = None
# Flow control
self._condition = Condition()
示例15: DeviceConnection
class DeviceConnection(object):
state_waiters = {}
state_happened = {}
def __init__ (self, device_server, stream, address):
self.fw_version = 0.0
self.recv_msg_cond = Condition()
self.recv_msg = {}
self.send_msg_sem = Semaphore(1)
self.pending_request_cnt = 0
self.device_server = device_server
self.stream = stream
self.address = address
self.stream.set_nodelay(True)
self.timeout_handler_onlinecheck = None
self.timeout_handler_offline = None
self.killed = False
self.sn = ""
self.private_key = ""
self.node_id = 0
self.iv = None
self.cipher_down = None
self.cipher_up = None
#self.state_waiters = []
#self.state_happened = []
self.event_waiters = []
self.event_happened = []
self.ota_ing = False
self.ota_notify_done_future = None
self.post_ota = False
self.online_status = True
@gen.coroutine
def secure_write (self, data):
if self.cipher_down:
cipher_text = self.cipher_down.encrypt(pad(data))
yield self.stream.write(cipher_text)
@gen.coroutine
def wait_hello (self):
try:
self._wait_hello_future = self.stream.read_bytes(64) #read 64bytes: 32bytes SN + 32bytes signature signed with private key
str1 = yield gen.with_timeout(timedelta(seconds=10), self._wait_hello_future,
io_loop=ioloop.IOLoop.current())
self.idle_time = 0 #reset the idle time counter
if len(str1) != 64:
self.stream.write("sorry\r\n")
yield gen.sleep(0.1)
self.kill_myself()
gen_log.debug("receive length != 64")
raise gen.Return(100) # length not match 64
if re.match(r'@\d\.\d', str1[0:4]):
#new version firmware
self._wait_hello_future = self.stream.read_bytes(4) #read another 4bytes
str2 = yield gen.with_timeout(timedelta(seconds=10), self._wait_hello_future, io_loop=ioloop.IOLoop.current())
self.idle_time = 0 #reset the idle time counter
if len(str2) != 4:
self.stream.write("sorry\r\n")
yield gen.sleep(0.1)
self.kill_myself()
gen_log.debug("receive length != 68")
raise gen.Return(100) # length not match 64
str1 += str2
self.fw_version = float(str1[1:4])
sn = str1[4:36]
sig = str1[36:68]
else:
#for version < 1.1
sn = str1[0:32]
sig = str1[32:64]
gen_log.info("accepted sn: %s @fw_version %.1f" % (sn, self.fw_version))
#query the sn from database
node = None
cur = self.device_server.cur
cur.execute('select * from nodes where node_sn="%s"'%sn)
rows = cur.fetchall()
if len(rows) > 0:
node = rows[0]
if not node:
self.stream.write("sorry\r\n")
yield gen.sleep(0.1)
self.kill_myself()
gen_log.info("node sn not found")
raise gen.Return(101) #node not found
key = node['private_key']
key = key.encode("ascii")
#.........这里部分代码省略.........