def _async_generator(): " Simple asynchronous generator. " # await. result = yield From(Future.succeed(1)) # yield yield AsyncGeneratorItem(result + 1) # await. result = yield From(Future.succeed(10)) # yield yield AsyncGeneratorItem(result + 1)
def wait_for_cpr_responses(self, timeout=1): """ Wait for a CPR response. """ cpr_futures = list(self._waiting_for_cpr_futures) # Make copy. # When there are no CPRs in the queue. Don't do anything. if not cpr_futures or self.cpr_support == CPR_Support.NOT_SUPPORTED: return Future.succeed(None) f = Future() # When a CPR has been received, set the result. def wait_for_responses(): for response_f in cpr_futures: yield From(response_f) if not f.done(): f.set_result(None) ensure_future(wait_for_responses()) # Timeout. def wait_for_timeout(): time.sleep(timeout) # Got timeout. if not f.done(): self._waiting_for_cpr_futures = deque() f.set_result(None) t = threading.Thread(target=wait_for_timeout) t.daemon = True t.start() return f
def write(self, message): """ Coroutine that writes the next packet. """ try: self.socket.send(message.encode('utf-8') + b'\0') except socket.error: if not self._closed: raise BrokenPipeError return Future.succeed(None)
def _run_in_terminal(self, func): # Make sure that when an application was active for this connection, # that we print the text above the application. with context(self._context_id): try: app = get_app(raise_exception=True) except NoRunningApplicationError: func() return Future.succeed(None) else: return app.run_in_terminal(func)
def async_func(): result = func() return Future.succeed(result)
def async_3(): " Succeed immediately. " return Future.succeed('Hello from async_3')
def other_coroutine(): value = yield From(Future.succeed(True)) value = yield From(Future.succeed(True)) raise Return('Result from coroutine.')