async def _wait_for_complete(self, result_future: asyncio.Future, *args, **kwargs): Logger.debug(tag=self._TAG, msg="_wait_for_complete() start") # Wait for rc to be ready future = self._engine.get_ready_future() await future with ThreadPoolExecutor(max_workers=1) as executor: # Call IconServiceEngine.hello() f = executor.submit(self._hello) future = asyncio.wrap_future(f) await future # Start to sync blocks f = executor.submit(self._run, *args, **kwargs) f = asyncio.wrap_future(f) for fut in asyncio.as_completed([f]): try: ret = await fut except Exception as exc: self._engine.close() # Wait to stop ipc_server for 1s await asyncio.sleep(1) result_future.set_exception(exc) else: self._engine.close() # Wait to stop ipc_server for 1s await asyncio.sleep(1) Logger.debug(tag=self._TAG, msg="_wait_for_complete() end1") result_future.set_result(ret) Logger.debug(tag=self._TAG, msg="_wait_for_complete() end2")
async def __wait_and_read_cache(event: asyncio.Event, fut: asyncio.Future, cache_file: Path): try: await event.wait() await CacheManager.__read_cache(fut, cache_file) except Exception as e: fut.set_exception(e)
def filter_stderr(popen:subprocess.Popen, future:asyncio.Future): last_ex = None while True: data = popen.stderr.readline() if data: # print("FFmpeg says:", data, flush=True) try: if check_stderr(data): sys.stderr.buffer.write(data) sys.stderr.buffer.flush() except FFmpegError as e: print("Error from FFmpeg:", e) last_ex = e except FFmpegWarning: pass # useless message else: break if last_ex: future.set_exception(last_ex) else: future.set_result(True)
class AIterator: def __init__(self): self.notifications = [] self.future = Future() source.pipe(ops.materialize()).subscribe(self.on_next) def feeder(self): if not self.notifications or self.future.done(): return notification = self.notifications.pop(0) dispatch = { 'N': lambda: self.future.set_result(notification.value), 'E': lambda: self.future.set_exception(notification. exception), 'C': lambda: self.future.set_exception( StopAsyncIteration) } dispatch[notification.kind]() def on_next(self, notification): self.notifications.append(notification) self.feeder() async def __anext__(self): self.feeder() value = await self.future self.future = Future() return value
async def generator(self, future: asyncio.Future = None): if self._loop is None: self._loop = asyncio.get_running_loop() stream = sd.InputStream(samplerate=self._sr, device=self._device, channels=self._channels, callback=self.__callback, dtype=self._dtype, blocksize=self._buffersize) with stream: if not stream.active: # if it was not called start() or exception was raised # in the audio callback if future: # if the future is waiting for the start or any failure # set the exception future.set_exception( f"Could not open the {self._device} capture device") # coroutine also will be notified raise MicrophoneCaptureFailed else: if future: # if the future is waiting for the start or any failure # set True meaning that the microphone was successfully opened future.set_result(True) while stream.active: indata, status = await self._buffer.get() yield indata.squeeze(), status
def _next(self): """ Returns a future for the next value in an iterator """ # ensure there is only one outstanding request at any given time, or segfault happens if cygrpc.OperationType.receive_message in self.stream._state.due: raise ValueError("Prior future was not resolved") future = Future() # this method is the same as the first part of _Rendevous._next with self.stream._state.condition: if self.stream._state.code is None: # Give grpc an event handler (self.handle_event) to call # whenever something changes with the stream # e.g. a batch is received or the channel is closed event_handler = lambda event: self.handle_event(event, future) self.stream._call.start_client_batch( (cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS), ), event_handler) self.stream._state.due.add( cygrpc.OperationType.receive_message) # if the stream is already finished (OK), just end now elif self.stream._state.code is grpc.StatusCode.OK: future.set_exception(StopAsyncIteration()) # if there's already an error in the stream, set the error and return else: future.set_exception(self.stream) return future
def set_future_result(future: Future, result: Any) -> None: # type: ignore # pypy if not future.done(): if isinstance(result, Exception): future.set_exception(result) else: future.set_result(result)
def filter_stderr(popen:subprocess.Popen, future:asyncio.Future): last_ex = None while True: data = popen.stderr.readline() if data: log.ffmpeg("Data from ffmpeg: {}".format(data)) try: if check_stderr(data): sys.stderr.buffer.write(data) sys.stderr.buffer.flush() except FFmpegError as e: log.ffmpeg("Error from ffmpeg: %s", str(e).strip()) last_ex = e except FFmpegWarning: pass # useless message else: break if last_ex: future.set_exception(last_ex) else: future.set_result(True)
def filter_stderr(popen:subprocess.Popen, future:asyncio.Future): last_ex = None while True: data = popen.stderr.readline() if data: # print("FFmpeg says:", data, flush=True) try: if check_stderr(data): sys.stderr.buffer.write(data) sys.stderr.buffer.flush() except FFmpegError as e: print("Error from FFmpeg:", e) last_ex = e except FFmpegWarning: pass # useless message else: break if last_ex: future.set_exception(last_ex) else: future.set_result(True)
def filter_stderr(popen:subprocess.Popen, future:asyncio.Future): last_ex = None while True: data = popen.stderr.readline() if data: log.ffmpeg("Data from ffmpeg: {}".format(data)) try: if check_stderr(data): sys.stderr.buffer.write(data) sys.stderr.buffer.flush() except FFmpegError as e: log.ffmpeg("Error from ffmpeg: %s", str(e).strip()) last_ex = e except FFmpegWarning: pass # useless message else: break if last_ex: future.set_exception(last_ex) else: future.set_result(True)
def as_future(fun, *args, **kwargs): """ Executes a function with the given arguments and wraps the result into a future. :param fun: The function to be called. :type fun: func :param args: The argument list for the supplied function. :type args: list :param kwargs: The keyword argument dict for the supplied function. :type kwargs: dict :return: The functions result wrapped in a Future :rtype: asyncio.Future """ try: res = fun(*args, **kwargs) except Exception as e: f = Future() f.set_exception(e) return f else: if isinstance(res, Future): return res elif iscoroutine(res): return asyncio.Task(res) else: f = Future() f.set_result(res) return f
def make_future(result=None, exception=None): future = Future() if exception: future.set_exception(exception) else: future.set_result(result) return future
def future(value=None, exception=None): f = Future() if exception: f.set_exception(exception) else: f.set_result(value) return f
async def _awaiter(future: Future) -> T: try: result = await future return result except asyncio.CancelledError as e: if not future.done(): future.set_exception(e) raise
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 set_result(future: Future, result: Any, exception: Exception) -> None: if future.done(): return if exception: future.set_exception(exception) else: future.set_result(result)
def ask_password(self, future: asyncio.Future): input_dialog = QtWidgets.QInputDialog() response = input_dialog.getText( self, "Password", "This server requires a password to join.") if not response[1]: future.set_exception(InterruptedError()) else: future.set_result(response[0])
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)
async def _execute_future(self, future: asyncio.Future, obj: object) -> Any: try: res = await self.func(obj) future.set_result(res) except Exception as e: future.set_exception(e) return await future
async def __read_cache(fut: asyncio.Future, cache_file: Path): try: async with aiofiles.open(cache_file, "rb") as f: b = await f.read() log.info(f"cache load from {cache_file}") fut.set_result(b) except Exception as e: fut.set_exception(e)
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)
async def awaiter(future: asyncio.Future) -> T: """等待future执行结束,返回执行结果 .""" try: result = await future return result except asyncio.CancelledError as e: if not future.done(): future.set_exception(e) raise
async def _wrap(coro: Coroutine, fut: Future) -> Future: """Used internally to tie a coroutine to a future with proper result/exception handling.""" try: result = await coro except Exception as ex: fut.set_exception(ex) else: fut.set_result(result) return fut
class Stream: def __init__(self): self._future = Future() self._complete = Event() self._listeners = [ ] def __aiter__(self): return self async def __anext__(self): if self._complete.is_set(): raise StopAsyncIteration result = await self._future self._future = Future() return result def write(self, item, last): if self._complete.is_set(): return if last: self._set_complete() if self._future.done(): self._future = Future() self._future.set_result(item) def abort(self, exc): if self._complete.is_set(): return self._set_complete() if self._future.done(): self._future = Future() self._future.set_exception(exc) def cancel(self): self._set_complete() if self._future.done(): self._future = Future() self._future.cancel() async def completed(self): await self._complete.wait() @property def is_complete(self): return self._complete.is_set() def add_complete_listener(self, listener): self._listeners.append(listener) def _set_complete(self): self._complete.set() for listener in self._listeners: listener()
async def _execute_select(self, sql: str, parameters: List, future: asyncio.Future): self.data.clear() try: with self.connection as con: for row in con.execute(sql, parameters): self.data.append(self._entity_factory(row)) future.set_result(len(self.data)) except sqlite3.DatabaseError as error: future.set_exception(error)
async def _runner(self, ident: str, coro: Awaitable[T_Result], future: asyncio.Future) -> None: try: result = await coro except Exception as e: future.set_exception(e) else: future.set_result(result) finally: del self._running[ident]
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 create_future_error(error=None): if error is None: error = create_failure() elif isinstance(error, Exception): error = FailedFuture(type(error), error, None) else: assert isinstance(error, IFailedFuture) f = Future() f.set_exception(error.value) return f
class PeersRequestCache(RandomNumberCache): def __init__(self, community, circuit, info_hash): super().__init__(community.request_cache, "peers-request") self.circuit = circuit self.info_hash = info_hash self.future = Future() def on_timeout(self): self.future.set_exception(RuntimeError("Peers request timeout"))
async def _spread_task(self, task_id: int, future: asyncio.Future): sql = """INSERT INTO PROCESSING_STEPS (TASK_ID, PARTNER_ID) SELECT TASKS.ID, SUBSCRIBERS.PARTNER_ID FROM TASKS, SUBSCRIBERS WHERE TASKS.ID = :ID AND TASKS.EVENT_ID = SUBSCRIBERS.EVENT_ID""" update_sql = """UPDATE TASKS SET STATE = 3, UPDATED_ON = datetime('now','localtime') WHERE ID = :ID""" try: with self.connection as con: con.execute(sql, {'ID': task_id}) con.execute(update_sql, {'ID': task_id}) future.set_result(True) except sqlite3.Error as error: future.set_exception(error)
def _done_callback(future: asyncio.Future, task: asyncio.Task): if task.cancelled(): future.cancel() return exception = task.exception() if exception is not None: future.set_exception(exception) return future.set_result(task.result())
def do_resolve(value_fut: asyncio.Future, known_fut: asyncio.Future, value: Any, is_known: bool, failed: Optional[Exception]): # Was an exception provided? If so, this is an abnormal (exceptional) resolution. Resolve the futures # using set_exception so that any attempts to wait for their resolution will also fail. if failed is not None: value_fut.set_exception(failed) known_fut.set_exception(failed) else: value_fut.set_result(value) known_fut.set_result(is_known)
def ensure_future(co): # type: ignore fut = Future() try: co.send(None) except StopIteration as v: result = v.args[0] if v.args else None fut.set_result(result) except BaseException as e: fut.set_exception(e) else: raise Exception("coroutine didn't finish in one pass") return fut
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 _as_future(fun, *args, **kwargs): try: res = fun(*args, **kwargs) except Exception as e: f = Future() f.set_exception(e) return f else: if isinstance(res, Future): return res elif iscoroutine(res): return asyncio.Task(res) else: f = Future() f.set_result(res) return f
async def _uwsgi_entry_point(self, current_greenlet: greenlet.greenlet, future: asyncio.Future, *args, **kwargs): """ uWSGI wrapper entry point. """ try: # Call the underlying wrapped function. result = await func(self, *args, **kwargs) except Exception as e: # Set the exception on the Future, allowing the web server to retrieve it. future.set_exception(e) else: # Set the result of the function on the Future. future.set_result(result) finally: # Switch back context. current_greenlet.switch()
def go(): error = Exception('woops') future = Future() future.set_exception(error) source = Observable.from_future(future) def on_next(x): success[0] = False def on_error(err): success[1] = str(err) == str(error) def on_completed(): success[2] = False subscription = source.subscribe(on_next, on_error, on_completed)
async def test_sends_ping(make_transport, make_fut): transp = make_transport() future = Future() future.set_result(False) ws = mock.Mock() ws.ping.side_effect = [future] hb_future = Future() hb_future.set_result((FRAME_HEARTBEAT, b"")) session_close_future = Future() session_close_future.set_exception(SessionIsClosed) session = mock.Mock() session._wait.side_effect = [hb_future, session_close_future] await transp.server(ws, session) assert ws.ping.called
async def _launcher(self, coro, callback_future: asyncio.Future = None): exception = None try: ret = await coro except Exception as e: exception = e pass finally: self._current_workers -= 1 while self._current_workers < self._max_workers: waiting_future = None while len(self._pending_queue) > 0: generator = self._pending_queue[0] try: waiting_future = next(generator) break except StopIteration: self._pending_queue.popleft() continue if waiting_future is None: break self._current_workers += 1 asyncio.ensure_future(waiting_future, loop=self._loop) if self._idle(): async with self._waiter: self._waiter.notify_all() if exception is not None: if callback_future is None: raise exception else: callback_future.set_exception(exception) return if callback_future is None: return ret else: callback_future.set_result(ret) return
async def test_run_coroutine_job(asyncio_scheduler, asyncio_executor, exception): from asyncio import Future, sleep future = Future() job = asyncio_scheduler.add_job(waiter, 'interval', seconds=1, args=[sleep, exception]) asyncio_executor._run_job_success = lambda job_id, events: future.set_result(events) asyncio_executor._run_job_error = lambda job_id, exc, tb: future.set_exception(exc) asyncio_executor.submit_job(job, [datetime.now(utc)]) events = await future assert len(events) == 1 if exception: assert str(events[0].exception) == 'dummy error' else: assert events[0].retval is True
class SubscriberThread(threading.Thread): def __init__(self, topic, id): super().__init__(target=self) self.subscriber = Subscriber(topic=topic, id=id) self.future = Future() def done(self): return self.future.done() def result(self): return self.future.result() def run(self): try: self.subscriber.open() result = self.loop() self.future.set_result(result) except Exception as error: self.future.set_exception(error) finally: self.subscriber.close() def loop(self): raise NotImplementedError()
class _ChunkedResponse(HTTPResponse): def __init__(self, status, reason, msg, sock): HTTPResponse.__init__(self, status, reason, msg) self.sock = sock self.chunk_read = None sock.loop.create_task(self.Chunks()) self.size = None async def read(self, amt): if not self.size: self.next_size = Future(loop=self.sock.loop) if self.chunk_read: self.chunk_read.set_result(None) try: [self.size, self.chunk_read] = await self.next_size except EOFError: return b"" data = await self.sock.recv(min(self.size, amt)) self.size -= len(data) return data async def Chunks(self): """ Generator that returns chunk length """ for _ in range(30000): parser = Parser(self.sock) await parser.next_char() if parser.c == b"\r": await parser.next_char() if parser.c == b"\n": await parser.next_char() await parser.space() size = 0 for _ in range(30): try: digit = int(parser.c, 16) except ValueError: break size = size * 16 + digit await parser.next_char() else: raise ExcessError("Chunk size of 30 or more digits") i = 0 while parser.c not in b"\n": # Including EOF i += 1 if i >= 3000: raise ExcessError("Line of 3000 or more characters") await parser.next_char() if not size: break chunk_read = Future(loop=self.sock.loop) self.next_size.set_result((size, chunk_read)) await chunk_read else: raise ExcessError("30000 or more chunks") self.next_size.set_exception(EOFError()) await parser.headers()
def func(): future = Future() future.set_exception(Exception(str(42))) return future
def _create_future_error(error=None): f = Future() f.set_exception(error) return f