async def Safe_5Gaction(client, userdata, msg): global message_received message_received = False async with Client(server) as client: async with client.filtered_messages(PosAttSub) as messages: await client.subscribe(PosAttSub) while True: try: message = await asyncio.wait_for(msg.__anext__(), timeout=5) message_received = True Msg = json.loads(msg.payload) # 将接收的订阅消息转换为字典格式 print(Msg) except asyncio.TimeoutError: if not message_received: DroneName = DroneSeries_to_num(Msg['Drone']) Safe_Pub_Msg = jsonstr_transform.GS_CMD_Pub(DroneName, 'Land') print(Safe_Pub_Msg) mqtt.push(CMDPub, Safe_Pub_Msg) csv_record.q_allmsg_csv_send(Safe_Pub_Msg, q_allmsg) time.sleep(5) else: message_received = False 代码报错RuntimeWarning: coroutine 'Safe_5Gaction' was never awaited callback(self, self._userdata, message) RuntimeWarning: Enable tracemalloc to get the object allocation traceback。如何解决
时间: 2024-02-10 10:26:19 浏览: 145
这个错误提示告诉我们 "coroutine 'Safe_5Gaction' was never awaited",意味着在代码中没有等待 `Safe_5Gaction` 这个协程的执行。要解决这个问题,你需要在调用 `Safe_5Gaction` 的地方使用 `await` 来等待它的执行。
假设你在某个地方调用了 `Safe_5Gaction`,你可以这样修改代码:
```python
await Safe_5Gaction(client, userdata, msg)
```
这样就能解决这个错误了。需要注意的是,你在调用 `Safe_5Gaction` 的地方也需要是一个协程(被 `async` 修饰的函数),或者在协程上下文中调用它。
另外,你还需要注意代码中的其他问题,比如 `PosAttSub` 和 `server` 的定义,以及相关的导入语句等。
阅读全文