async def forward_usb_to_net(): while True: packet = await iface.read(flush=False) if len(packet) >= 14: # must be at least ETH_HLEN, or we'll get EINVAL self.logger.debug("usb->net packet=<%s>", dump_hex(packet)) await net_iface.send(packet) else: self.logger.warn("usb->net length=%d discarded", len(packet)) async def forward_net_to_usb(): while True: packet = await net_iface.recv() self.logger.debug("net->usb packet=<%s>", dump_hex(packet)) await iface.write(packet) await iface.flush() done, pending = await asyncio.wait([ asyncio.create_task(forward_usb_to_net()), asyncio.create_task(forward_net_to_usb()) ], return_when=asyncio.FIRST_EXCEPTION) for task in done: await task