我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用asyncio.InvalidStateError()。
def await_reply(self) : "retrieves the Message. If it is not yet available, suspends the" \ " coroutine (letting the event loop do other things) until it becomes" \ " available. On a timeout, libdbus will construct and return an error" \ " return message." assert self._conn.loop != None, "no event loop on parent Connection to attach coroutine to" if self._wrap_notify != None or self._awaiting != None : raise asyncio.InvalidStateError("there is already a notify set on this PendingCall") #end if done = self._conn.loop.create_future() self._awaiting = done def pending_done(pending, _) : done.set_result(self.steal_reply()) #end pending_done self.set_notify(pending_done, None) reply = await done return \ reply #end await_reply
def connect(self, addr=None, net_opts=None): self._disconnecting = False if self.connected: if (addr is None or addr == self.addr) and (net_opts is None or net_opts == self.net_opts): # already connected as expected return raise asyncio.InvalidStateError( 'already connected to different addr, disconnect before connect to else where' ) if addr is not None: self.addr = addr if net_opts is not None: self.net_opts = None if not self.addr: raise asyncio.InvalidStateError('attempt connecting without addr') # this can be called from any thread self._loop.call_soon_threadsafe(self._loop.create_task, self._connect())
def process_result(self, result): for t in self.results: if t.done() or t.cancelled(): continue try: t.set_result(result.pop(0)) except (KeyError, IndexError, AttributeError): t.set_result(False) except asyncio.InvalidStateError: continue
def test_cancel(self): f = asyncio.Future(loop=self.loop) self.assertTrue(f.cancel()) self.assertTrue(f.cancelled()) self.assertTrue(f.done()) self.assertRaises(asyncio.CancelledError, f.result) self.assertRaises(asyncio.CancelledError, f.exception) self.assertRaises(asyncio.InvalidStateError, f.set_result, None) self.assertRaises(asyncio.InvalidStateError, f.set_exception, None) self.assertFalse(f.cancel())
def test_result(self): f = asyncio.Future(loop=self.loop) self.assertRaises(asyncio.InvalidStateError, f.result) f.set_result(42) self.assertFalse(f.cancelled()) self.assertTrue(f.done()) self.assertEqual(f.result(), 42) self.assertEqual(f.exception(), None) self.assertRaises(asyncio.InvalidStateError, f.set_result, None) self.assertRaises(asyncio.InvalidStateError, f.set_exception, None) self.assertFalse(f.cancel())
def test_exception(self): exc = RuntimeError() f = asyncio.Future(loop=self.loop) self.assertRaises(asyncio.InvalidStateError, f.exception) f.set_exception(exc) self.assertFalse(f.cancelled()) self.assertTrue(f.done()) self.assertRaises(RuntimeError, f.result) self.assertEqual(f.exception(), exc) self.assertRaises(asyncio.InvalidStateError, f.set_result, None) self.assertRaises(asyncio.InvalidStateError, f.set_exception, None) self.assertFalse(f.cancel())
def force_finish(self) -> 'any': """Force the join session to finish and the heist to start. Returns any Anything Raises ------ Exception Whatever :class:`JoinSession.do_heist` raises. RuntimeError If no task is provided to the session. """ guild = self.ctx.guild log.info(f'Forcing a finish at {guild!s}[{guild.id}]') self.finish.set() if self.task is None: raise RuntimeError('wat') # since Task.exception() returns None # I had to setup err as a string by default err = 'nothing' while err == 'nothing': try: log.info(f'waiting for exception {self.finish.is_set()}') err = self.task.exception() except asyncio.InvalidStateError: await asyncio.sleep(1) if err is not None: raise err log.info('returning result') return self.task.result()
def _set_reply(self, correlation_id, msg): if correlation_id in self._futures: try: self._futures[correlation_id].set_result(msg) except asyncio.InvalidStateError as e: LOGGER.error( 'Attempting to set result on already-resolved future: %s', str(e))
def _fail_reply(self, correlation_id, err): if correlation_id in self._futures and \ not self._futures[correlation_id].done(): try: self._futures[correlation_id].set_exception(err) except asyncio.InvalidStateError as e: LOGGER.error( 'Attempting to set exception on already-resolved future: ' '%s', str(e))
def _connect(self): assert self.addr, 'no addr has been specified ?!' if self._wire: raise asyncio.InvalidStateError('requesting new connection with transport already wired') try: if isinstance(self.addr, (str, bytes)): # UNIX domain socket connection transport, protocol = await self._loop.create_unix_connection( lambda: SocketProtocol(self), self.addr, ) else: # TCP socket addr_family = socket.AF_INET # default to IPv4 only if self.net_opts is not None: addr_family = self.net_opts.get('family', addr_family) transport, protocol = await self._loop.create_connection( lambda: SocketProtocol(self), self.addr['host'], self.addr['port'], **self.net_opts or {}, family=addr_family, ) assert protocol is self._wire and transport is protocol.transport, 'conn not made atm ?!' except Exception as exc: fut = self._conn_fut if fut is not None: self._conn_fut = None fut.set_exception(exc)