0

I’m working on a project in Python were a serial device is connected and communicates over network with other devices. I use the asyncio module for asynchronous communication.

My idea:

Each incoming message gets parsed and the device address is added to a participants list. After the participant is added I want to send an OK back to the sender. For sending and receiving messages I use the asyncio.Queue. There is my problem, after I added the address I cannot put my ok message into the Queue for sending a received message back to the sender. I get I do not know where to put/call the ack_msg(). The error message I get is:

RuntimeWarning: coroutine 'Table.add_address' was never awaited

This is my first project in Python and I guess I miss something basic in how Python works. I shortened the code to the basic receiving sending functions.

 import asyncio import Parser class Communicator(asyncio.Protocol): def __init__(self, received_queue, transmit_queue): super().__init__() self.buffer = None self.transport = None self.parser = Parser() self.table = Table(self) self.received_queue = received_queue self.transmit_queue = transmit_queue def connection_made: # Code to setup connect async def ack_msg(): # Acknowledment message code def data_received(self, line: for line in lines[:-1]: self.parser.parse_message(line, self.transport) received_queue = asyncio.Queue() transmit_queue = asyncio.Queue() communicator_partial = partial(Communicator, received_queue, transmit_queue) loop = asyncio.get_event_loop() communicator = serial_asyncio.create_serial_connection(loop, communicator_partial, '/dev/ttys005', baudrate=115200) asyncio.ensure_future(print_received(loop, received_queue)) asyncio.ensure_future(read_prompt(loop, transmit_queue, communicator_partial)) loop.run_forever() loop.close() import Table class Parser(): def __init__(self, communicator): self.table = Table(communicator) def parse_message(): # Code to cute the address from String self.table.add_address(source) import asyncio class Table() def __init__(self, communicator): self.table = dict() self.communicator = communicator async def add_address(self, address, hop, metric): if address not in self.routing_table: self.routing_table[address] = Node(address) await self.communicator.transmit_queue.put(self.communicator.ack_msg()) 
1
  • There are some identication problems in your code, could you fix it please? Commented Dec 7, 2019 at 11:26

1 Answer 1

1

ack_msg is async function, you need to await it:

await self.communicator.transmit_queue.put(await self.communicator.ack_msg()) 

you can read more here from the accepted answer

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.