def _handle_future(future: Future, result: object, exception: Exception): if future.cancelled(): return if exception is not None: future.set_exception(exception) else: if future.cancelled(): _LOGGER.debug("Skipping set_result for cancelled future.") elif future.done(): _LOGGER.error("This future is already done: cannot set result.") else: future.set_result(result)
def run_until_complete(self, future: asyncio.Future) -> Any: self._check_running() new_task = False if not asyncio.isfuture(future): future = asyncio.ensure_future(future, loop=self) # We wrapped `future` in a new Task since it was not a Future. new_task = True # An exception is raised if the new task doesn't complete, so there is no need to log the "destroy # pending task" message. future._log_destroy_pending = False else: if _helpers.get_future_loop(future) is not self: raise ValueError("Future does not belong to this loop") future.add_done_callback(_run_until_complete_cb) try: self._run_mainloop() except: if new_task and future.done() and not future.cancelled(): # The coroutine raised a BaseException. Consume the exception to not log a warning (Future # will log a warning if its exception is not retrieved), the caller doesn't have access to the # task wrapper we made. future.exception() raise finally: future.remove_done_callback(_run_until_complete_cb) if not future.done(): raise RuntimeError('Event loop stopped before Future completed.') return future.result()
def check_for_exceptions(done_future: asyncio.Future): if not done_future.cancelled() and done_future.exception(): logger_for_error.error('Future {}done with exception!'.format( '' if not future_description else '({}) '. format(future_description)), exc_info=make_exc_info( done_future.exception()))
def _cafit_done(self, fut: asyncio.Future) -> None: if fut.cancelled(): self.cancel() if self.done(): return result: ConanFitResult = fut.result() self.left_contact = result.left_contact self.right_contact = result.right_contact self.left_angle = result.left_angle self.right_angle = result.right_angle self.left_curvature = result.left_curvature self.right_curvature = result.right_curvature self.left_arc_center = result.left_arc_center self.right_arc_center = result.right_arc_center self.left_arclengths = result.left_arclengths self.right_arclengths = result.right_arclengths self.left_residuals = result.left_residuals self.right_residuals = result.right_residuals self.left_mask = result.left_mask self.right_mask = result.right_mask self.status = ConanAnalysisStatus.FINISHED self.job_end = time.time()
def feature_finished(self, future: asyncio.Future, reconnect_fn=None, name=''): try: self.logInfo('Feature finished: "{}"'.format(name)) if future.cancelled(): return exc = future.exception() if exc: if (isinstance(exc, ConnectionClosed) and exc.code > 1002) \ or isinstance(exc, InvalidStatusCode, TypeError): self.logError(exc) if reconnect_fn and not self.stop: self.logInfo('Trying to reconnect...') sleep(1) reconnect_fn() else: self.logInfo('No reconnection function...') return else: self.logError(exc) else: ws: WebSocketClientProtocol = future.result() if ws and ws.state == State.CLOSED: self.logInfo('WS Closed: {} - {}'.format( ws.close_code, ws.close_reason)) except: self.logError(traceback.format_exc())
def _run_coro(future: asyncio.Future, coro: Awaitable[Any], callback: Callable[[str, bool], Awaitable[Any]]) -> None: loop = asyncio.new_event_loop() task = loop.create_task(coro) userge.loop.call_soon_threadsafe( future.add_done_callback, lambda _: (loop.is_running( ) and future.cancelled() and loop.call_soon_threadsafe(task.cancel))) try: ret, exc = None, None with redirect() as out: try: ret = loop.run_until_complete(task) except asyncio.CancelledError: return except Exception: # pylint: disable=broad-except exc = traceback.format_exc().strip() output = exc or out.getvalue() if ret is not None: output += str(ret) loop.run_until_complete(callback(output, exc is not None)) finally: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close() userge.loop.call_soon_threadsafe( lambda: future.done() or future.set_result(None))
def _wait_handler(self, _future: asyncio.Future, _keys: List[EventHandlerKey], **kwargs): for key in _keys: self.remove_handler_by_key(key) if _future.cancelled(): return _future.set_result(kwargs)
def run_forever(self, sub_keepalive: asyncio.Future) -> None: """ Start the asyncio loop, running until it is either SIGTERM-ed or killed by keyboard interrupt. The Future parameter is used to cancel subscription Future in the case that an unexpected exception is thrown. You can also directly pass the `.subscribe()` method call instead like so: sub.run_forever(sub.subscribe(callback)) """ try: self.loop.run_forever() except (KeyboardInterrupt, concurrent.futures.CancelledError): pass finally: # 1. stop the `SubscriberClient` future, which will prevent more # tasks from being leased if not sub_keepalive.cancelled(): sub_keepalive.cancel() # 2. cancel the tasks we already have, which should just be # `worker` instances; note they have # `except CancelledError: pass` for task in asyncio.Task.all_tasks(loop=self.loop): task.cancel() # 3. stop the `asyncio` event loop self.loop.stop()
def _wait_handler(self, _future: asyncio.Future, _keys: [str], **kwargs): for key in _keys: self.remove_handler_by_key(key) if _future.cancelled(): return _future.set_result(result=kwargs)
def handle_response(self, fut: Future): peername = self.transport.get_extra_info('peername') print('sending to:{}'.format(peername)) if fut.cancelled(): return response = fut.result() self.transport.write(response.encode()) self.setup_callback()
def callback(fut: Future): if fut.cancelled(): return if fut.exception() is not None: create_task(loop, coroutine, __task__=task, restart_on=restart_on)
def _handle_future(future: Future, result: object, exception: Exception): if future.cancelled(): return if exception is not None: future.set_exception(exception) else: future.set_result(result)
def _callback(fut: asyncio.Future, result: Any, errorno: int) -> None: if fut.cancelled(): return if errorno is not None: fut.set_exception( error.DNSError(errorno, pycares.errno.strerror(errorno))) else: fut.set_result(result)
def _runner_cb(fut: asyncio.Future) -> None: if not fut.cancelled(): exc = fut.exception() if exc and not isinstance(exc, (SystemExit, KeyboardInterrupt)): loop.default_exception_handler({'exception': exc}) loop.stop()
def _fulfill_future(fut: asyncio.Future, result, err: Exception = None): """Resolve a callback future given the result and exception, if any.""" if fut.cancelled(): LOGGER.debug("callback previously cancelled") elif err: fut.set_exception(err) else: fut.set_result(result)
def _stop_task(key: str, future: asyncio.Future) -> None: if not future.cancelled(): future.cancel() logging.info(f"stop task:{key}") elif future.done(): logging.warning(f"task:{key} already stop") else: logging.warning(f"{key} can't stop")
def _create_job_callback(job: Job, fut: asyncio.Future): exc = fut.exception() if not fut.cancelled() and exc: raise exc if fut.done(): job.meta_update(result=fut.result()) job.save()
def _source_read_done(self, fut: asyncio.Future) -> None: if fut.cancelled(): self.cancel() return if self.done(): return image, timestamp = fut.result() self._image_ready(image, timestamp)
def coro_finished(f: asyncio.Future): if f.cancelled(): self.logger.warning('begin of trx is cancelled') return e = f.exception() if e is not None and not isinstance(e, pymysql.err.OperationalError): self.logger.error('Exception happened while beginning trx: %s', str(e), exc_info=e)
def _log_future_error(self, future: asyncio.Future): # Technically the task housing the task this callback is for is what's # usually cancelled, therefore cancelled() doesn't actually catch this case try: if future.cancelled(): return elif exc := future.exception(): self.bot.loop.create_task(self.log_error(exc)) except asyncio.CancelledError: return
def future_done(other: asyncio.Future, current: asyncio.Future): if current.cancelled(): other.set_exception(RuntimeError("Canceled.")) elif current.exception(): other.set_exception(current.exception()) else: payload = current.result() payload.data = b'(server ' + payload.data + b')' payload.metadata = b'(server ' + payload.metadata + b')' other.set_result(payload)
def _hdl_input_image_read(self, read_task: Future) -> None: if read_task.cancelled(): self.cancel() return if self.bn_is_done.get(): return image, image_timestamp = read_task.result() self._start_fit(image, image_timestamp)
def callback(my_future: Future): if not my_future.done() or my_future.cancelled(): return result: ExResult = my_future.result() if result.solver_result.satisfiable: return else: print("kept graph") with open("results_random_planar_110_filtered.json", mode="a") as f: print(simplejson.dumps(result), file=f)
def run_job_done_callback(f: asyncio.Future): if f.cancelled(): if not job.done(): job.cancel() elif f.exception(): job.set_exception(f.exception()) elif f.done(): job.set_result(f.result()) # cancel should be triggered by job else: raise RuntimeError(f"Unexpected future {f}")
def _handle_startup_done(fut: asyncio.Future) -> None: if fut.cancelled(): safe_shutdown(ioloop, ibl.stop) else: exc = fut.exception() if exc is not None: LOGGER.exception("Server setup failed", exc_info=exc) traceback.print_exception(type(exc), exc, exc.__traceback__) safe_shutdown(ioloop, ibl.stop) else: LOGGER.info("Server startup complete")
def _mark_stale(self, future: asyncio.Future): """ Marks child process as stale depending of keepalive result.""" if future.cancelled(): # child exited return if future.result(): # clean pipe reader termination return # keep-alive task returned False, marking child as not alive. self.logger.warning("Marking %s as stale" % self._child_pid) self._alive = False
def on_read(read_chunks: asyncio.Queue, fut: asyncio.Future): if fut.cancelled(): return elif fut.exception(): read_chunks.put_nowait('EOF') else: line = fut.result() if not line or not line.endswith(b'\n'): # EOF received - line is empty or incomplete read_chunks.put_nowait('EOF') line = line.decode('utf-8').rstrip('\n') read_chunks.put_nowait(line)
def _done_callback(self, future: asyncio.Future, request=None): try: if future.cancelled(): self._write_500(request) return exception = self._get_future_exception(future) if exception: traceback.print_exception(None, exception, exception.__traceback__) self._write_500(request) finally: self._futures.remove(future)
async def runner(): nonlocal acquired, has_result, result waiter = Future() waiters.append(waiter) maybe_acquire() try: await waiter except CancelledError: if waiter.done() and not waiter.cancelled(): acquired = False maybe_acquire() raise if has_result: return result try: result = await func(*args) except CancelledError: acquired = False maybe_acquire() raise except BaseException as exception: acquired = False while waiters: waiter = waiters.popleft() if not waiter.cancelled(): waiter.set_exception(exception) raise else: acquired = False has_result = True while waiters: waiter = waiters.popleft() if not waiter.cancelled(): waiter.set_result(None) return result
def _set_wait_future_result(result_future: asyncio.Future, wait_future: asyncio.Future) -> None: if wait_future.cancelled(): return try: result = result_future.result() set_func = wait_future.set_result except (Exception, asyncio.CancelledError) as e: result = e set_func = wait_future.set_exception set_func(result)
def _send_request(self, app_name, endpoint, entity, params): packet = MessagePacket.request(self.name, self.version, app_name, _Service._REQ_PKT_STR, endpoint, params, entity) future = Future() request_id = params['request_id'] self._pending_requests[request_id] = future try: self.tcp_bus.send(packet) except ClientException as e: if not future.done() and not future.cancelled(): ERROR = '101_Client not found' exception = RequestException(ERROR) exception.error = ERROR future.set_exception(exception) _Service.time_future(future, TCPServiceClient.REQUEST_TIMEOUT_SECS) return future
def _send_request(self, app_name, endpoint, entity, params, timeout): packet = MessagePacket.request(self.name, self.version, app_name, _Service._REQ_PKT_STR, endpoint, params, entity) future = Future() request_id = params['request_id'] self._pending_requests[request_id] = future try: self.tcp_bus.send(packet) except ClientException: if not future.done() and not future.cancelled(): error = 'Client not found' exception = ClientException(error) exception.error = error future.set_exception(exception) _Service.time_future(future, timeout) return future
def get(self): """Remove and return an item from the channel. If channel is empty, wait until an item is available. This method is a coroutine. """ while self.empty() and not self._close.is_set(): getter = Future(loop=self._loop) self._getters.append(getter) try: yield from getter except ChannelClosed: raise except: getter.cancel() # Just in case getter is not done yet. if not self.empty() and not getter.cancelled(): # We were woken up by put_nowait(), but can't take # the call. Wake up the next in line. self._wakeup_next(self._getters) raise return self.get_nowait()
def put(self, item): """Put an item into the channel. If the channel is full, wait until a free slot is available before adding item. If the channel is closed or closing, raise ChannelClosed. This method is a coroutine. """ while self.full() and not self._close.is_set(): putter = Future(loop=self._loop) self._putters.append(putter) try: yield from putter except ChannelClosed: raise except: putter.cancel() # Just in case putter is not done yet. if not self.full() and not putter.cancelled(): # We were woken up by get_nowait(), but can't take # the call. Wake up the next in line. self._wakeup_next(self._putters) raise return self.put_nowait(item)
def _on_task_done(task: Future) -> None: tasks.remove(task) if not task.cancelled() and task.exception(): self._render_future.set_exception(task.exception())
def _remove_done_future(self, fut: Future, *, req_id: int) -> None: self._futures.pop(req_id, None) if not fut.cancelled() and fut.exception(): self._render_future.set_exception(fut.exception())
def check_for_exceptions(done_future: asyncio.Future): if not done_future.cancelled() and done_future.exception(): logger_for_error.error('Future {}done with exception!'.format('' if not future_description else '({}) '.format(future_description)), exc_info=make_exc_info(done_future.exception()))