首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过异步队列发送确认消息

通过异步队列发送确认消息
EN

Stack Overflow用户
提问于 2019-12-07 11:03:31
回答 1查看 222关注 0票数 0

我正在用Python做一个项目,一个串行设备连接起来,并通过网络与其他设备进行通信。我使用异步模块进行异步通信。

我的想法:

每个传入的消息都被解析,设备地址被添加到参与者列表中。在参与者被添加之后,我想把一个OK返回给发件人。对于发送和接收消息,我使用asyncio.Queue。这是我的问题,在我添加了地址之后,我无法将我的ok message放入Queue中,因为它将接收到的消息发送回发件人。我知道我不知道把/打电话给ack_msg()。我得到的错误消息是:

RuntimeWarning:人们从未期待过coroutine 'Table.add_address‘

这是我在Python中的第一个项目,我想我忽略了Python如何工作的一些基本内容。我把代码缩短为基本的接收发送功能。

代码语言:javascript
运行
复制
    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())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-07 11:39:16

ack_msgasync函数,您需要await它:

代码语言:javascript
运行
复制
await self.communicator.transmit_queue.put(await self.communicator.ack_msg())

您可以从已接受的答案中读取更多的here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59225303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档