我正在用Python做一个项目,一个串行设备连接起来,并通过网络与其他设备进行通信。我使用异步模块进行异步通信。
我的想法:
每个传入的消息都被解析,设备地址被添加到参与者列表中。在参与者被添加之后,我想把一个OK返回给发件人。对于发送和接收消息,我使用asyncio.Queue。这是我的问题,在我添加了地址之后,我无法将我的ok message放入Queue中,因为它将接收到的消息发送回发件人。我知道我不知道把/打电话给ack_msg()。我得到的错误消息是:
RuntimeWarning:人们从未期待过coroutine 'Table.add_address‘
这是我在Python中的第一个项目,我想我忽略了Python如何工作的一些基本内容。我把代码缩短为基本的接收发送功能。
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())发布于 2019-12-07 11:39:16
ack_msg是async函数,您需要await它:
await self.communicator.transmit_queue.put(await self.communicator.ack_msg())您可以从已接受的答案中读取更多的here。
https://stackoverflow.com/questions/59225303
复制相似问题