Apply Queue
This commit is contained in:
3
main.py
3
main.py
@@ -41,7 +41,8 @@ def main():
|
||||
robot.sendTextMsg("机器人启动成功!", "filehelper")
|
||||
|
||||
# 接收消息
|
||||
robot.enableRecvMsg()
|
||||
# robot.enableRecvMsg() # 可能会丢消息?
|
||||
robot.enableReceivingMsg() # 加队列
|
||||
|
||||
# 每天 7 点发送天气预报
|
||||
robot.onEveryTime("07:00", weather_report, robot=robot)
|
||||
|
||||
17
robot.py
17
robot.py
@@ -4,6 +4,8 @@ import logging
|
||||
import re
|
||||
import time
|
||||
import xml.etree.ElementTree as ET
|
||||
from queue import Empty
|
||||
from threading import Thread
|
||||
|
||||
from wcferry import Wcf, WxMsg
|
||||
|
||||
@@ -139,6 +141,21 @@ class Robot(Job):
|
||||
def enableRecvMsg(self) -> None:
|
||||
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:
|
||||
""" 发送消息
|
||||
:param msg: 消息字符串
|
||||
|
||||
Reference in New Issue
Block a user