async def test_call_asyncio_ctx(self, loop): self.did_it = 0 async with aio_as_trio(AioContext(self, loop), loop=loop) as ctx: assert ctx.parent is self assert self.did_it == 2 self.did_it = 3 assert self.did_it == 4
async def test_trio_asyncio_iterator(self, loop): async def slow_nums(): for n in range(1, 6): asyncio.sleep(0.01, loop=loop) yield n sum = 0 async for n in aio_as_trio(slow_nums()): sum += n assert sum == 15
async def test_trio_asyncio_iter(self, loop): sth = SomeThing(loop) n = 0 if sys.version_info >= (3, 7): assert sniffio.current_async_library() == "trio" async for x in aio_as_trio(sth.iter_asyncio()): n += 1 assert x == n assert n == 2 assert sth.flag == 1
async def test_trio_asyncio_iterator(self, loop): @async_generator async def slow_nums(): for n in range(1, 6): await asyncio.sleep(0.01, loop=loop) await yield_(n) sum = 0 async for n in aio_as_trio(slow_nums()): sum += n assert sum == 15
async def test_trio_asyncio_iterator_depr(self, loop): async def slow_nums(): for n in range(1, 6): await asyncio.sleep(0.01) yield n sum = 0 # with test_utils.deprecate(self): ## not yet async for n in aio_as_trio(slow_nums(), loop=loop): sum += n assert sum == 15
async def _receiver(self, *, task_status=trio.TASK_STATUS_IGNORED): read = trio_asyncio.aio_as_trio(self.reader.read) decoders = Registry() @decoders.register(Reply.SHUTDOWN) def decode_shutdown(cmd: List[int]) -> HardwareUpdate: raise NotImplemented @decoders.register(Reply.EMERGENCY_STOP) def decode_emergency_stop(cmd: List[int]) -> HardwareUpdate: raise NotImplemented @decoders.register(Reply.MOTOR_DONE_UPDATE) def decode_motor_done_update(cmd: List[int]) -> HardwareUpdate: raise NotImplemented @decoders.register(Reply.UART_UPDATE) def decode_uart_update(cmd: List[int]) -> HardwareUpdate: raise NotImplemented async def read_command() -> List[int]: cmd = await read(1) if cmd[0] not in (Reply.SUCCESS_REPLIES | Reply.ERROR_REPLIES | Reply.UPDATES): raise RuntimeError(f"HWC speaks a language we don't understand: 0x{cmd[0]:02X}") if cmd[0] in Reply.LENGTHS: length = Reply.LENGTHS[cmd[0]] if length > 1: cmd += await read(length - 1) else: cmd += await read(1) length = cmd[1] + 2 if length > 2: cmd += await read(length - 2) if len(cmd) != length: raise TruncatedCommandError(f"HWC sent a truncated response: {' '.join(f'{b:02X}' for b in cmd)}") return list(cmd) task_status.started() while True: # TODO handle TruncatedCommandError # TODO will this abort when there's nothing to receive? We have a receive timeout configured... logger.debug(f"Listening for HWC message") cmd = await read_command() logger.debug(f"Got HWC message: {' '.join(f'{b:02X}' for b in cmd)}") if cmd[0] in Reply.UPDATES: decode = decoders[cmd[0]] self._enqueue_update(decode(cmd)) else: await self._replies_in.send(cmd)
async def test_trio_asyncio_ctx(self, loop): sth = SomeThing(loop) async with aio_as_trio(sth.ctx_asyncio()): assert sth.flag == 1 assert sth.flag == 3
sms_mailings = await db.get_sms_mailings("1") print("sms_mailings") print(sms_mailings) async def send(): while True: await asyncio.sleep(1) await redis.publish("updates", sms_id) async def listen(): *_, channel = await redis.subscribe("updates") while True: raw_message = await channel.get() if not raw_message: raise ConnectionError("Connection was lost") message = raw_message.decode("utf-8") print("Got message:", message) await asyncio.gather(send(), listen()) finally: redis.close() await redis.wait_closed() if __name__ == "__main__": trio_asyncio.run(trio_asyncio.aio_as_trio(main))