Marta
Marta

Reputation: 183

Sending acknowledgment messages via asyncio queues

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())

Upvotes: 0

Views: 477

Answers (1)

nonamer92
nonamer92

Reputation: 1927

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

Upvotes: 1

Related Questions