Apply Queue

This commit is contained in:
Changhua
2023-07-15 07:48:48 +08:00
parent 0819dc37aa
commit 04ae9b8b5a
2 changed files with 19 additions and 1 deletions

View File

@@ -41,7 +41,8 @@ def main():
robot.sendTextMsg("机器人启动成功!", "filehelper") robot.sendTextMsg("机器人启动成功!", "filehelper")
# 接收消息 # 接收消息
robot.enableRecvMsg() # robot.enableRecvMsg() # 可能会丢消息?
robot.enableReceivingMsg() # 加队列
# 每天 7 点发送天气预报 # 每天 7 点发送天气预报
robot.onEveryTime("07:00", weather_report, robot=robot) robot.onEveryTime("07:00", weather_report, robot=robot)

View File

@@ -4,6 +4,8 @@ import logging
import re import re
import time import time
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from queue import Empty
from threading import Thread
from wcferry import Wcf, WxMsg from wcferry import Wcf, WxMsg
@@ -139,6 +141,21 @@ class Robot(Job):
def enableRecvMsg(self) -> None: def enableRecvMsg(self) -> None:
self.wcf.enable_recv_msg(self.onMsg) self.wcf.enable_recv_msg(self.onMsg)
def enableReceivingMsg(self) -> None:
def innerProcessMsg(wcf: Wcf):
while wcf.is_receiving_msg():
try:
msg = wcf.get_msg()
self.LOG.info(msg)
self.processMsg(msg)
except Empty:
continue # Empty message
except Exception as e:
self.LOG.error(f"Receiving message error: {e}")
self.wcf.enable_receiving_msg()
Thread(target=innerProcessMsg, name="GetMessage", args=(self.wcf,), daemon=True).start()
def sendTextMsg(self, msg: str, receiver: str, at_list: str = "") -> None: def sendTextMsg(self, msg: str, receiver: str, at_list: str = "") -> None:
""" 发送消息 """ 发送消息
:param msg: 消息字符串 :param msg: 消息字符串