我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tornado.gen.moment()。
def _server_request_loop(self, delegate): try: while True: conn = HTTP1Connection(self.stream, False, self.params, self.context) request_delegate = delegate.start_request(self, conn) try: ret = yield conn.read_response(request_delegate) except (iostream.StreamClosedError, iostream.UnsatisfiableReadError): return except _QuietException: # This exception was already logged. conn.close() return except Exception: gen_log.error("Uncaught exception", exc_info=True) conn.close() return if not ret: return yield gen.moment finally: delegate.on_close(self)
def wait_for_event(self, **event_args): # Demented python scope. # http://stackoverflow.com/questions/4851463/python-closure-write-to-variable-in-parent-scope # This variable could be an object, but instead it's a single-element list. event_matched = [] @gen.coroutine def mark_true(event): event_matched.append(event) log.info('Creating a temporary listener for %s' % (event_args,)) self.event_listeners.append((event_args, mark_true)) while not event_matched: yield gen.moment log.info('Deleting the temporary listener for %s' % (event_args,)) self.event_listeners.remove((event_args, mark_true)) raise gen.Return(event_matched[0])
def _get_next_event(self): """Slack-specific message reader. Returns a web event from the API listener if available, otherwise waits for the slack streaming event. """ if len(self._web_events): event = self._web_events.pop() raise gen.Return(event) # TODO: rewrite this logic to use `on_message` feature of the socket # FIXME: At the moment if there are 0 socket messages then web_events # will never be handled. message = yield self.connection.read_message() log.debug('Slack message: "%s"' % message) message = json.loads(message) raise gen.Return(message)
def test_handle_stream_coroutine_logging(self): # handle_stream may be a coroutine and any exception in its # Future will be logged. class TestServer(TCPServer): @gen.coroutine def handle_stream(self, stream, address): yield gen.moment stream.close() 1 / 0 server = client = None try: sock, port = bind_unused_port() with NullContext(): server = TestServer() server.add_socket(sock) client = IOStream(socket.socket()) with ExpectLog(app_log, "Exception in callback"): yield client.connect(('localhost', port)) yield client.read_until_close() yield gen.moment finally: if server is not None: server.stop() if client is not None: client.close()
def test_nonblocking_put_with_getters(self): q = queues.Queue() get0 = q.get() get1 = q.get() q.put_nowait(0) # put_nowait does *not* immediately unblock getters. yield gen.moment self.assertEqual(0, (yield get0)) q.put_nowait(1) yield gen.moment self.assertEqual(1, (yield get1))
def test_task_done_delay(self): # Verify it is task_done(), not get(), that unblocks join(). q = self.queue_class() q.put_nowait(0) join = q.join() self.assertFalse(join.done()) yield q.get() self.assertFalse(join.done()) yield gen.moment self.assertFalse(join.done()) q.task_done() self.assertTrue(join.done())
def async_exception(self, e): yield gen.moment raise e
def start(self): if self._web_app: log.info('Starting web app.') self._start_web_app() log.info('Executing the start scripts.') for function in self._on_start: log.debug('On Start: %s' % function.__name__) yield function() log.info('Bot started! Listening to events.') while True: event = yield self._get_next_event() log.debug('Received event: %s' % event) log.debug('Checking against %s listeners' % len(self.event_listeners)) event_matched = False # Note: Copying the event_listeners list here to prevent # mid-loop modification of the list. for kwargs, function in list(self.event_listeners): match = self._check_event_kwargs(event, kwargs) log.debug('Function %s requires %s. Match: %s' % ( function.__name__, kwargs, match)) if match: event_matched = True # XXX Rethink creating a chat object. Only using it for error handling chat = yield self.event_to_chat(event) future = function(event=event) handle_exceptions(future, chat) # Figure out why this was added yield gen.moment if not event_matched: # Add no-match handler. Mainly for Fallbakc like API.AI # Maybe add @bot.fallback() ? # But there should be only one fallback handler pass
def _get_next_event(self): if len(self._web_events): event = self._web_events.pop() raise gen.Return(event) while not self.input_line: yield gen.moment user_input = self.input_line self.input_line = None event = {'type': 'message', 'text': user_input} raise gen.Return(event)
def listen_for(self, regex): self.listening = regex # Hang until self.hear() sets this to False self.bot._add_listener(self) while self.listening: yield gen.moment self.bot._remove_listener(self) raise gen.Return(self.heard_message)
def test_handle_stream_coroutine_logging(self): # handle_stream may be a coroutine and any exception in its # Future will be logged. class TestServer(TCPServer): @gen.coroutine def handle_stream(self, stream, address): yield gen.moment stream.close() 1/0 server = client = None try: sock, port = bind_unused_port() with NullContext(): server = TestServer() server.add_socket(sock) client = IOStream(socket.socket()) with ExpectLog(app_log, "Exception in callback"): yield client.connect(('localhost', port)) yield client.read_until_close() yield gen.moment finally: if server is not None: server.stop() if client is not None: client.close()