本文整理汇总了Python中tornado.locks.Condition.wait方法的典型用法代码示例。如果您正苦于以下问题:Python Condition.wait方法的具体用法?Python Condition.wait怎么用?Python Condition.wait使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.locks.Condition
的用法示例。
在下文中一共展示了Condition.wait方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_data
# 需要导入模块: from tornado.locks import Condition [as 别名]
# 或者: from tornado.locks.Condition import wait [as 别名]
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))
示例2: Window
# 需要导入模块: from tornado.locks import Condition [as 别名]
# 或者: from tornado.locks.Condition import wait [as 别名]
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)
示例3: GameScrapperWorker
# 需要导入模块: from tornado.locks import Condition [as 别名]
# 或者: from tornado.locks.Condition import wait [as 别名]
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)
示例4: PingHandler
# 需要导入模块: from tornado.locks import Condition [as 别名]
# 或者: from tornado.locks.Condition import wait [as 别名]
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()
示例5: test_future_close_callback
# 需要导入模块: from tornado.locks import Condition [as 别名]
# 或者: from tornado.locks.Condition import wait [as 别名]
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()
示例6: ImporterWorker
# 需要导入模块: from tornado.locks import Condition [as 别名]
# 或者: from tornado.locks.Condition import wait [as 别名]
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)
示例7: InMemStream
# 需要导入模块: from tornado.locks import Condition [as 别名]
# 或者: from tornado.locks.Condition import wait [as 别名]
class InMemStream(Stream):
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
def clone(self):
new_stream = InMemStream()
new_stream.state = self.state
new_stream.auto_close = self.auto_close
new_stream._stream = deque(self._stream)
return new_stream
def read(self):
def read_chunk(future):
if self.exception:
future.set_exception(self.exception)
return future
chunk = ""
while len(self._stream) and len(chunk) < common.MAX_PAYLOAD_SIZE:
chunk += self._stream.popleft()
future.set_result(chunk)
return future
read_future = tornado.concurrent.Future()
# We're not ready yet
if self.state != StreamState.completed and not len(self._stream):
wait_future = self._condition.wait()
wait_future.add_done_callback(
lambda f: f.exception() or read_chunk(read_future)
)
return read_future
return read_chunk(read_future)
def write(self, chunk):
if self.exception:
raise self.exception
if self.state == StreamState.completed:
raise UnexpectedError("Stream has been closed.")
if chunk:
self._stream.append(chunk)
self._condition.notify()
# This needs to return a future to match the async interface.
r = tornado.concurrent.Future()
r.set_result(None)
return r
def set_exception(self, exception):
self.exception = exception
self.close()
def close(self):
self.state = StreamState.completed
self._condition.notify()
示例8: get_data
# 需要导入模块: from tornado.locks import Condition [as 别名]
# 或者: from tornado.locks.Condition import wait [as 别名]
def get_data(cls, account, source_filter, limit=100, skip=0):
source_filter = OneDriveFileFilter(source_filter)
if source_filter.file is None:
raise ValueError("required parameter file missing")
app_log.info("Starting to retrieve file for {}".format(account._id))
client = AsyncHTTPClient()
uri = "https://api.onedrive.com/v1.0/drive/items/{}/content".format(source_filter.file)
lock = Condition()
def crawl_url(url):
# some yummy regex
location_header_regex = re.compile(r"^Location:\s?(?P<uri>http:/{2}\S+)")
http_status_regex = re.compile(r"^HTTP/[\d\.]+\s(?P<status>\d+)")
receiving_file = False
# define our callbacks
def header_callback(header):
m = http_status_regex.match(header)
if m is not None:
# process our HTTP status header
status = m.group("status")
if int(status) == 200:
# if we're 200, we're receiving the file, not just a redirect
app_log.info("Receiving file {} for account {}".format(source_filter.file, account._id))
global receiving_file
receiving_file = True
m = location_header_regex.match(header)
if m is not None:
# process our location header
uri = m.group("uri")
# and grab _that_ url
app_log.info("Following redirect for file {}".format(source_filter.file))
crawl_url(uri)
def stream_callback(chunk):
# only dump out chunks that are of the file we're looking for
global receiving_file
if receiving_file:
app_log.info("Writing chunk of {}B".format(chunk.__len__()))
cls.write(chunk)
def on_completed(resp):
if 200 <= resp.code <= 299:
lock.notify()
oauth_client = account.get_client()
uri, headers, body = oauth_client.add_token(url)
req = HTTPRequest(
uri, headers=headers, body=body, header_callback=header_callback, streaming_callback=stream_callback
)
client.fetch(req, callback=on_completed)
crawl_url(uri)
# wait for us to complete
try:
yield lock.wait(timeout=timedelta(seconds=MAXIMUM_REQ_TIME))
app_log.info("File {} retrieved successfully".format(source_filter.file))
except gen.TimeoutError:
app_log.error("Request for file {} => {} timed out!".format(source_filter.file, account._id))
示例9: PeerGroup
# 需要导入模块: from tornado.locks import Condition [as 别名]
# 或者: from tornado.locks.Condition import wait [as 别名]
class PeerGroup(object):
"""A PeerGroup represents a collection of Peers.
Requests routed through a PeerGroup can be sent to either a specific peer
or a peer chosen at random.
"""
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()
def __str__(self):
return "<PeerGroup peers=%s>" % str(self._peers)
@gen.coroutine
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
try:
yield [peer.close() for peer in self._peers.values()]
finally:
self._peers = {}
self._resetting = False
self._reset_condition.notify_all()
def get(self, hostport):
"""Get a Peer for the given destination.
A new Peer is added and returned if one does not already exist for the
given host-port. Otherwise, the existing Peer is returned.
"""
assert hostport, "hostport is required"
if hostport not in self._peers:
self._peers[hostport] = Peer(self.tchannel, hostport)
return self._peers[hostport]
def lookup(self, hostport):
"""Look up a Peer for the given host and port.
Returns None if a Peer for the given host-port does not exist.
"""
assert hostport, "hostport is required"
return self._peers.get(hostport, None)
def remove(self, hostport):
"""Delete the Peer for the given host port.
Does nothing if a matching Peer does not exist.
:returns: The removed Peer
"""
assert hostport, "hostport is required"
return self._peers.pop(hostport, None)
def add(self, peer):
"""Add an existing Peer to this group.
A peer for the given host-port must not already exist in the group.
"""
assert peer, "peer is required"
if isinstance(peer, basestring):
# Assume strings are host-ports
peer = Peer(self.tchannel, peer)
assert peer.hostport not in self._peers, (
"%s already has a peer" % peer.hostport
)
#.........这里部分代码省略.........