def run(self): time.sleep(10) myDebug.Print("Spider Quote Thread Start...") while (self.source.setTime() == False): time.sleep(30) #myDebug.Print('Spider Quote Stoped.\n -- not quote time.\n') #myDebug.Print("nSpider Quote Thread ReStart...") #self.stop() #return myDebug.Print('Spider Quote Run...') self.threadRunning = True while self.threadRunning: try: #发生错误时继续 parms = self.getParams() lstReturn = self.source.query(params=parms) self.output(lstReturn) time.sleep(self.interval) #判断结束 if (self.source._Stoped()): break except Exception as ex: pass self.stop()
def _Check_Connection(self): #TCP是否关闭或正在关闭,则重连 isReConnect = False #if(self.usrConn.is_closed or self.usrConn.is_closing): if (self.usrConn.is_closed): self.Init() isReConnect = True #重建频道 if (isReConnect or self.usrChannel.is_closed): usrType = myData.iif(self.isSender, "生产者", "消费者") myDebug.Print("消息队列MQ::", usrType + ">> " + self.nameQueue + "已经掉线!") #在连接上创建一个频道 #self.usrChannel = self.usrConn.channel() #声明一个队列,生产者和消费者都要声明一个相同的队列,用来防止万一某一方挂了,另一方能正常运行 #self.usrChannel.queue_declare(queue=self.nameQueue) try: self.Init_Queue(self.nameQueue, self.isDurable, self.isAutoAck) time.sleep(2) myDebug.Print("消息队列MQ::", usrType + ">> " + self.nameQueue + "已重连!") except Exception as ex: myDebug.Print("消息队列MQ::", usrType + ">> " + self.nameQueue + "重连失败!") myError.Error(ex) pass self.isInited = self.usrConn.is_open #标识是否在线
def SwapData_In(self, nStepSwaps=1): nums = 0 lstDatas = [] nStepSwaps = myData.iif(nStepSwaps <= 0, sys.maxsize, nStepSwaps) lstFiles = myIO.getFiles(self.dirSwap, wildcard=".json", iswalk=False) if (len(lstFiles) < 1): return lstDatas myDebug.Print("DataSwap IOFiles::") myDebug.Print("\t" + self.dirSwap) myDebug.Print("\tSwap IOFiles(" + str(len(lstFiles)) + ")") for file in lstFiles: fileName = myIO.getFileName(file, True) if (fileName[0:len(self.tagName)] != self.tagName): continue if (self.checkNeedAck(fileName)): continue # 超时校检 if (self.delayedTime > 0): #解析时间并校检 timeTag = fileName[len(self.tagName) + 1:] timeData = myData_Trans.Tran_ToDatetime( timeTag, "%Y-%m-%d-%H-%M-%S") dtNow = datetime.datetime.now() if ((dtNow - timeData).seconds > self.delayedTime): self.SwapData_BackUp(file) continue #读取文件内容 myDebug.Print("\tnew file swap:: " + myIO.getFileName(file, False)) strJson = myIO.getContent(file, noBOM=True) pJson = myData_Json.Json_Object() pJson.Trans_FromStr(strJson) #组装交换信息 data = {"path": file, "fileInfo": pJson._dict_} lstDatas.append({"tagAck": fileName, "data": data}) #记录Ackinfo if (self.useAck): ackInfo = { "isAcked": False, "time": datetime.datetime.now(), "retrys": 0, "path": file } self.ackDict[fileName] = ackInfo else: self.SwapData_BackUp(file) nums += 1 if (nums >= nStepSwaps): break myDebug.Print("DataSwap IOFiles End." + "\tMargin Swap IOFiles(" + str(len(lstFiles) - len(lstDatas)) + ")\n") return lstDatas
def run(self): #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 myDebug.Print("Starting " + self.name) i = 0 while not self.exitFlag: self.ind = 0 pMMdata_M, self.ind = self.managerMMap.Read(0, False) if (pMMdata_M != None): #发送消息 dict0 = pMMdata_M.value itchat.send(dict0["Text"], dict0["FromUserName"]) #itchat.send('%s: %s' % (typeMsg, msgInfo), userFrom) myDebug.Print("Exiting Manager MMap.")
def Init_MMap(self, useCmdMMap=True): # 创建内存映射(读) try: self.managerMMap = myMMap.myMMap_Manager(self.dirData + "zxcMMap.dat") pMMdata_M2, ind2 = self.managerMMap.Read(0) if (pMMdata_M2 != None): myDebug.Print(pMMdata_M2.value, ind2) return True except: print() myDebug.Print("创建内存映射失败.") return False
def OnHandleMsg(self, msg, plat = "", bCheck = False, nSleep = 0): if(msg == None): return strMsg = msg.get('msg') if(strMsg == '~__~'): myDebug.Print("消息管理器::", typePlatform + ">> ", "心跳::" + strMsg) return #异步延迟消息处理 if(nSleep > 0): self.OnHandleMsg_Check(msg, plat) msg["timeLimit"] = datetime.datetime.now() + datetime.timedelta(seconds=nSleep) self.usrMsgs_Buffer.append(msg) return #消息内容校正 if(bCheck): self.OnHandleMsg_Check(msg, plat) #文字输出 typePlatform = myData.iif(plat == "", msg.get("usrPlat", myMsgPlat.wx), plat) if(self.usePrint): myDebug.Print("消息管理器::", typePlatform + ">> ",strMsg) #声音输出 #if(self.useVoice): # myVoice.Say_thrd(msg["text"]) #在线消息输出 if(typePlatform != ""): pWeb = self.usrWebs.get(typePlatform, None) #提取对应平台的web对象 if(pWeb != None): #格式化接口输出 wxPath = msg["usrName"] + "/" + strMsg + "/" + msg["msgType"] pWeb.Do_API_get(wxPath, typePlatform + "API-py") usrMQ = self.usrMQs.get(typePlatform, None) if(usrMQ != None): #特殊消息处理 toUser = msg.get('to_usrName', "") if(toUser != ""): toUser = myData.iif(toUser.lower() != "self", toUser, self.usrNameSelfs.get(typePlatform, toUser)) msg['usrID'] = "" msg['usrName'] = toUser #调整toUser msg['usrNameNick'] = "" #转发消息 usrMQ.Send_Msg(usrMQ.nameQueue, str(msg)) if(self.usePrint): myDebug.Print("消息管理器转发::", usrMQ.nameQueue + ">> ",strMsg)
def saveData_stream_Trans(self, strDir = ""): #提取当前数据 if(strDir == ""): strDir = self.dir dictDatas = {} if(True): myDebug.Print("... load data(" + self.name + ")...") path = strDir + self.fileName + ".csv" pDt_csv = myIO_xlsx.DtTable() pDt_csv.dataFieldType = ['datetime', 'float', 'float', 'float'] #数据字段类型集 pDt_csv.Load_csv(path, isUtf = True) #组装数据 nRows = len(pDt_csv.dataMat) for x in range(nRows - 1, -1, -1): pData = copy.deepcopy(self.data) pData.datetime = None #清空时间(必须) pData.fromValueList(pDt_csv.dataMat[x]) #由表数据还原 dictDatas[pData.getTime()] = pData #字典排序 keys = list(dictDatas.keys()) keys.sort(key = None, reverse = True) #组装数据 pDt = myIO_xlsx.DtTable() pDt.dataField = self.data.csvHead().split(',') pDt.dataMat = [] for x in keys: pDt.dataMat.append(dictDatas[x].toValueList()) #保存基础数据 myIO.mkdir(strDir) pDt.Save(strDir, self.fileName, row_start = 0, col_start = 0, cell_overwrite = True, sheet_name = self.name, row_end = -1, col_end = -1, bSave_AsStr = False)
def _Done_Text(self, Text, msgID="", usrInfo={}): #提取命令内容 if (Text.count("@*") != 1): return "" cmds = Text.strip()[2:].split(" ") cmd = cmds[0].strip() nNum = len(cmds) myDebug.Print(Text.strip()) #账单命令处理 strReturn = "" if (cmd == "帮助"): return self._Title_Helper() elif (cmd.count("*") == 1): #消息交互 return self._Done_Text_Buffer(Text, "TEXT", usrInfo) elif (cmd == ""): #机器人 return self.myRobot._Done(Text, msgID, "TEXT", usrInfo) elif (cmd == "股票"): #发送股票设置界面链接 usrPlat, usrID = self._Done_Check_UserBack(usrInfo) url = gol._Get_Setting( "serverUrl" ) + 'zxcWebs/stock/quoteset/' + usrID + "/" + usrPlat strReturn = quote(url, safe=string.printable) # unquote elif (cmd == "订单"): #发送订单相关界面链接 usrPlat, usrID = self._Done_Check_UserBack(usrInfo) orderType = cmds[1].strip() url = gol._Get_Setting( "serverUrl" ) + 'zxcWebs/order/Add/' + orderType + "?usrID=" + usrID strReturn = quote(url, safe=string.printable) # unquote return strReturn
def _Title(self, usrInfo={}, bRegist=False, bRegistOut=False): if (self.isRunning): if (bRegist or bRegistOut): groupName = usrInfo.get('groupName', "") strReturn = myData.iif(groupName == "", "", "@" + usrInfo.get('usrName', "") + ":") if (bRegistOut == False): strReturn += self.doTitle + "功能" + "--已注册\n\t" + self._Title_User_Opened( ) + "(" + str(self.tStart) + ")" else: strReturn += self.doTitle + "功能" + "--已注销\n\t" + self._Title_User_Opened( ) + "(" + str(self.tStart) + ")" else: self.isRunning = False #标识非运行 self.isValid = True #有效性恢复 strReturn = self.doTitle + "功能" + "--已关闭\n\t" + self._Title_User_Closed( ) + "(" + str(self.tStart) + ")" else: self.Init() #初始基础信息 self.isRunning = True #标识运行 self.usrID = usrInfo.get('usrID', "") #功能所属用户ID(启动者) self.usrName = usrInfo.get('usrName', "") #功能所属用户名称(启动者) strReturn = self.doTitle + "功能" + "--已开启\n\t" + self._Title_User_Opened( ) + "(" + str(self.tStart) + ")" myDebug.Print(strReturn + "\n") return strReturn
def OnHandleMsg_ByThread(self): try: #线程循环 while self.isRuning: try: #循环所有缓存进行处理 for x in self.usrMsgs_Buffer: bNeedSend = False time = x.get("timeLimit", "") if (time == "" and myData_Trans.Is_Numberic(time)): bNeedSend = True else: if (type(time) != datetime.datetime): time = myData_Trans.Tran_ToDatetime(time) time_N = datetime.datetime.now() if (time_N >= time): bNeedSend = True #消息发送 if (bNeedSend == False): continue x['timeLimit'] = "" self.OnHandleMsg(x, "") self.usrMsgs_Buffer.remove(x) # 延时 self.OnHandleMsg_ByThread_sleep(0.5) except: myDebug.Error("Err:: Run_OnHandleMsg_ByThread... ") except: myDebug.Error("Err:: Run_OnHandleMsg_ByThread... Restart...") myDebug.Print('Thread Run_OnHandleMsg_ByThread is exiting...')
def callback_Consumer(self, ch, method, properties, body): #回调,通知处理消息 if (self.callback_RecvMsg != None): try: strMsg = body.decode('utf-8') if (strMsg == self.strHeartbeat): ch.basic_ack(delivery_tag=method.delivery_tag ) #接收到消息后会给rabbitmq发送一个确认 #myDebug.Print("消息队列MQ::", self.nameQueue + ">> ", "接受心跳::" + strMsg) return #消息回调 self.callback_RecvMsg(strMsg) ch.basic_ack( delivery_tag=method.delivery_tag) #接收到消息后会给rabbitmq发送一个确认 except Exception as ex: usrType = myData.iif(self.isSender, "生产者", "消费者") myDebug.Print("消息队列MQ::", usrType + ">> " + self.nameQueue + "连接中断!") myError.Error(ex) finally: #if(self.isAutoAck == False): # #不论当前消息是否成功,都表示消息确实处理完了 手动确认 否则没有ack不再发送新消息 保证确实被处理了再确认 # ch.basic_ack(delivery_tag = method.delivery_tag) #接收到消息后会给rabbitmq发送一个确认 pass
def Done_ByMsg(self, msg, bOnHandleMsg = False): myDebug.Print("请求消息:: ", msg) if(msg == None): return None #提取消息内容(自定义格式类型 myManager_Msg.OnCreatMsg) usrID = msg.get('usrID', "") usrName = msg.get('usrName', "") usrNameNick = msg.get('usrNameNick', "") nameSelf = msg.get('usrNameSelf', "") groupID = msg.get('groupID', "") groupName = msg.get('groupName', "") msgText = msg.get('msg', "") msgID = msg.get('msgID', "") msgType = msg.get('msgType', "") plat = msg.get('usrPlat', "") #按消息类型进一步处理('TEXT', 'IMAGE', 'VOICE', 'VIDEO') if(msgType == myManager_Msg.myMsgType.TEXT): msgText = msgText elif(msgType == myManager_Msg.myMsgType.PICTURE): #图片文件进行解析 msgText = msgText #调用 msgRs = self.Done(usrID, usrName, usrNameNick, msgText, msgID, msgType, plat, groupID, groupName, nameSelf) #推送结果至消息管理器 if(bOnHandleMsg): for msgR in msgRs: #处理所有返回消息,忽略无内容的 if(msgR == None): continue if(msgR.get('msg', "") == ""): continue self.OnHandleMsg(msgR) #消息处理 myDebug.Debug("处理消息::", msgR) return msgRs
def Run_ByThread(self, nSleep=0.1): try: #创建微信线程 self.thrd_Replay = threading.Thread(target=itchat.run) self.thrd_Replay.setDaemon(False) self.thrd_Replay.start() #创建消息队列 self.isRuning = True self.Init_MQ(bStart=True) #线程循环 while self.isRuning: try: #运行(监测, 用于线程内部) self.Run_Monitor() #命令监测--共享内存方式 if (self.useCmdMMap): self.Run_Monitor_Cmd_ByMMP() time.sleep(nSleep) except: myDebug.Error("Err:: Run_Monitor... ") except: myDebug.Error("Err:: Run_ByThread... Restart...") myDebug.Print('Thread is exiting...')
def _LogionOuted(self): myDebug.Print('系统已退出') #掉线重连 if (True): #登录微信网页版(二维码扫码) self.Logion() #运行 pWeixin.Run_ByThread()
def run(self): if (self.source.setTime() == False): myDebug.Print('StockQuote stoped.\n --not stock time..') self.stop() return myDebug.Print('StockQuote run') self.threadRunning = True while self.threadRunning: try: #发生错误时继续 self.source.query() #判断结束 if (self.source._Stoped()): break time.sleep(self.interval) except: pass self.stop()
def __init__(self): self.usrTag = "" self.usrName = "" self.usrNameNick = "" self.usrReplys = myRoot_Usr.myRoot_Usrs("", "") #消息用户集 self.usrMMsg = gol._Get_Setting('manageMsgs') #消息管理器 self.usrMQ_Recv = None #消息队列队形 self.isRunning = False #是否运行中 self._Init() #按全局权限初始 #self.wxDos = {} #消息处理类 #self.wxUser_Root = None #当前授权用户对象(避免频繁查找) myDebug.Print("消息处理工厂--已初始 (%s::%s--%s)" % (self.usrName, self.usrNameNick, self.usrTag))
def _Init_MQ(self, bStart=True): #初始消息接收队列 self.mqName = 'zxcMQ_robot' if (self.mqRecv == None): self.mqRecv = myMQ_Rabbit.myMQ_Rabbit(False) self.mqRecv.Init_Queue(self.mqName, True) self.mqRecv.Init_callback_RecvMsg(self.callback_RecvMsg) #消息接收回调 myDebug.Print("消息队列(" + self.mqName + ")创建成功...") #接收消息 if (bStart): self.mqRecv.Start()
def stop(self): myDebug.Print('StockQuote stop\n') self.threadRunning = False self.stopped = True time.sleep(2) #退出 strDir, strName = myIO.getPath_ByFile(__file__) strDir_Base = os.path.abspath(os.path.join(strDir, "..")) strPath = strDir_Base + "/myQuote_API.lock" gol._Run_UnLock(strPath) exit(0)
def init_MQ(self, bStart=False): #初始消息接收队列 self.mqName = 'zxcMQ_usrWin' if (self.mqRecv == None): self.mqRecv = myMQ_Rabbit.myMQ_Rabbit(False) self.mqRecv.Init_Queue(self.mqName, True) self.mqRecv.Init_callback_RecvMsg(self.callback_RecvMsg) #消息接收回调 myDebug.Print("消息队列(" + self.mqName + ")创建成功...") #接收消息 if (bStart): self.mqRecv.Start() self.mqTimeNow = myData_Trans.Tran_ToTime_int() #接收开始时间
def mainStart(): #线程执行 pQuote = gol._Get_Value('quoteSource') if (pQuote == None): pQuote = mainSource() thrdQuote = gol._Get_Value('quoteSourceThread', None) if (pQuote != None and (thrdQuote == None or thrdQuote.threadRunning == False)): myDebug.Print("Quote thread start...") thrdQuote = Quote_Thread(pQuote) thrdQuote.setDaemon(True) thrdQuote.start() gol._Set_Value('quoteSourceThread', thrdQuote)
def _Done_Text(self, Text, msgID = "", usrInfo = {}): #提取命令内容@¥* if(Text.count("@¥*") != 1): return "" cmds = Text.strip()[3:].split(" ") cmd = cmds[0].strip() nNum = len(cmds) myDebug.Print(Text.strip()) #命令处理 strReturn = "" if(cmd == "帮助"): return self._Title_Helper() elif(cmd == "风控"): return self._Done_Text_set风控(cmd, cmds, usrInfo) return strReturn
def _Monitor_Consumer(self): while True: try: self._Check_Connection() time.sleep(self.nHeartbeat) #主动发送内容,模拟心跳 if (self.isSender): self.Send_Msg(self.nameQueue, self.strHeartbeat) #myDebug.Print("消息队列MQ::", self.nameQueue + ">> ", "发送心跳::" + self.strHeartbeat) except Exception as ex: usrType = myData.iif(self.isSender, "生产者", "消费者") myDebug.Print("消息队列MQ::", usrType + ">> " + self.nameQueue + "连接中断!") myError.Error(ex) pass
def SwapData_OutFile(self, tagName="", dirDest=""): dataOut = copy.copy(self.dataOut) self.dataOut = [] if (len(dataOut) < 1): return False jsonData = myData_Json.Json_Object(dataOut) dirOut = myData.iif(dirDest != "", dirDest, self.dirSwap_out) + "/" if (tagName == ""): tagName = self.tagName if (tagName == "" and dirOut == ""): return False fileName = tagName + myData_Trans.Tran_ToDatetime_str( None, "_%Y_%m_%d_%H_%M_%S") + ".json" content = jsonData.ToString(autoFormat=False).replace("\\", "/") myIO.Save_File(dirOut + fileName, content, True, False) #myDebug.Print("DataSwap Out IOFiles::" + dirOut + fileName) myDebug.Print("DataSwap Out IOFiles::" + fileName) return True
def Init_MQ(self, bStart=False): if (gol._Get_Value("msgSet_usrMQ", False) == False): self.Init_Swap(bStart) return #初始消息接收队列 self.mqName = 'zxcMQ_wx' if (self.mqRecv == None): self.mqRecv = myMQ_Rabbit.myMQ_Rabbit(False) self.mqRecv.Init_Queue(self.mqName, True) self.mqRecv.Init_callback_RecvMsg(self.callback_RecvMsg) #消息接收回调 myDebug.Print("消息队列(" + self.mqName + ")创建成功...") #接收消息 if (bStart): self.mqRecv.Start() self.mqTimeNow = myData_Trans.Tran_ToTime_int() #接收开始时间
def Run_Monitor_Cmd_ByMMP(self): if (self.managerMMap == None): return False nNum = 0 pMMdata_M, self.ind = self.managerMMap.Read(self.ind, True) while (pMMdata_M != None): #调用发送消息 nNum += 1 msg = pMMdata_M.value if (msg != None and msg != 0): self.Send_Msg(msg['usrID'], msg['usrName'], msg['msg'], msg['msgType']) myDebug.Print(msg) #再次提取命令 if (nNum >= self.max): return pMMdata_M, self.ind = self.managerMMap.Read(self.ind, True)
def checkTime(self, data=None): if (data == None): tNow = datetime.datetime.now() if (self.startTime < tNow and tNow < self.endTime): return True else: if (tNow > self.endTime): self.startTime = myData_Trans.Tran_ToDatetime( self.dtDay + " 13:00:00") #起始时间 self.endTime = self.endTime2 #结束时间 if (self.startTime < tNow and tNow < self.endTime): return True return False #时间判断 tNow = data.datetime_queryed if (self.startTime < tNow and tNow < self.endTime): if (self.timeIntervals > 0): self.timeIntervals += 1 return True elif (self.timeIntervals == 0 and self.endTime < tNow): self.startTime = myData_Trans.Tran_ToDatetime( self.dtDay + " 13:00:00") #起始时间 self.endTime = self.endTime2 #结束时间 self.timeIntervals += 1 if (self.datasNow != None): self.datasNow.saveData() #保存数据(第一时段结束) return self.checkTime(data) elif (self.endTime < tNow): if (self.datas.get(data.name, None) == None): #初始数据(当天结束时段后,只有一条数据) self.timeIntervals += 1 return True elif (self.timeIntervals > 1): #设置数据监听停止 if (self.datasNow.name != data.name): self.datasNow = self.datas.get(data.name, None) if (self.datasNow.stoped == False): self.datasNow.saveData() #保存数据(第二时段结束) self.datasNow.stoped = True myDebug.Print("... stoped data(" + data.name + ")...") return False
def Init_Swap(self, bStart=False): #初始消息接收-Swap self.swapName = 'zxcSwap_wx' if (self.swapRecv == None): self.swapRecv = gol._Get_Value('dataSwap_msgWx') self.swapOut = gol._Get_Value('dataSwap_msgWx_out') @self.swapRecv.changeDataSwap() def Reply(lstData): for x in lstData: if (self.Done_Swap(x)): self.swapRecv.ackDataSwap(x) pass myDebug.Print("消息Swap(" + self.swapName + ")创建成功...") #接收消息 if (bStart): self.swapOut.startSwap() self.swapRecv.startSwap() self.mqTimeNow = myData_Trans.Tran_ToTime_int() #接收开始时间
def _Done_Text(self, Text, msgID = "", usrInfo = {}): #提取命令内容@* if(Text.count("@*") != 1): return "" cmds = Text.strip()[2:].split(" ") cmd = cmds[0].strip() nNum = len(cmds) myDebug.Print(Text.strip()) #命令处理 strReturn = "" if(cmd == "帮助"): return self._Title_Helper() elif(cmd == "排名"): if("墨紫" == usrInfo.get('usrNameNick', "")): #提取排名信息 pDB = gol._Get_Setting('zxcdbStockReturns') nTop = 10 if(len(cmds) > 1 and myData_Trans.Is_Numberic(cmds[1])): nTop = myData_Trans.To_Int(cmds[1]) lstRanks = pDB.Get_Ranks(nTop = nTop) strRanks = "年收益排名(" + str(datetime.datetime.now().year) + "):" + myData_Trans.Tran_ToStr(lstRanks, "\r\n") return strRanks return strReturn
def _OnHandle_Iot_CmdsState_Monitorthread(self): # 循环所有执行中命令 try: # 延时2秒 time.sleep(2) #线程循环 while self.isRuning: try: # Iot命令状态校检-所有执行中 self._checkIot_CmdsState_doing() # 延时2秒 time.sleep(2) except: myDebug.Error( "Err:: Run_OnHandle_Iot_CmdsState_Monitor_ByThread... " ) except: myDebug.Error( "Err:: Run_OnHandle_Iot_CmdsState_Monitor_ByThread... Restart..." ) myDebug.Print( 'Thread Run_OnHandle_Iot_CmdsState_Monitor_ByThread is exiting...')
myListener_Rise_Fall_asInt.Quote_Listener_Rise_Fall_asInt()) #for x in pSets.setUsers: # pQuote.addListener(myListener_FixedMonitor.Quote_Listener_FixedMonitor(x)) gol._Set_Value('quoteSource', pQuote) #实例 行情对象 return pQuote #主启动程序 if __name__ == "__main__": #单例运行检测 if (gol._Run_Lock(__file__) == False): exit(0) #示例数据监控(暂只支持单源,多源需要调整完善) mainSource() pQuote = gol._Get_Value('quoteSource') pSets = gol._Get_Value('setsQuote') #线程执行 thread = Quote_Thread(pQuote) thread.setDaemon(True) thread.start() mainloop(thread) time.sleep(1) myDebug.Print("Quote thread exited...") #退出 #gol._Run_UnLock(__file__) exit(0)