Exemplo n.º 1
0
    def __init__(self,
                 strFullTaskName,
                 strTaskParam="",
                 lTaskParam1=0,
                 lTaskParam2=0,
                 iCheckEveryMilSecond=1000,
                 iIdleCheckEveryMilSecond=5000,
                 bExRemoteCallMe=False,
                 bTemporyUse=False):
        # 获得最后唯一ID
        self.s_lUniqueID = FiberMang.GetUniqueInstanceID()

        self.s_strFullTaskName = strFullTaskName
        self.s_strTaskParam = strTaskParam
        self.s_lTaskParam1 = lTaskParam1
        self.s_lTaskParam2 = lTaskParam2

        # 时间检查
        self.s_iCheckEveryMilSecond = iCheckEveryMilSecond  # 每单位时间检查一次
        self.s_iIdleCheckEveryMilSecond = iIdleCheckEveryMilSecond  # 如果持续空闲,则休眠这个时间段

        self.s_lastFreeMilTimeDiff = CSkBot_Common_Share.CSkBot_MilTimeIdentify(
            self.s_iCheckEveryMilSecond)  # 空闲检查
        self.s_lastContinueIdle_MilTimeDiff = CSkBot_Common_Share.CSkBot_MilTimeIdentify(
            self.s_iIdleCheckEveryMilSecond)  # 空闲检查
        self.s_bLastBusyStatus = False
        self.s_bInContinueCheckFreeStatus = False
        self.s_iKeepIdleCheckFitCount = 0  # 持续统计空闲次数
        self.s_iMaxKeepIdleCheckCount = 20  # 最多持续空闲20次

        self.s_parentFiberMang = None

        self.s_bExRemoteCallMe = bExRemoteCallMe
        self.s_bTemporyUse = bTemporyUse
Exemplo n.º 2
0
    def ScheduleExecDelayPostMsg(self, iMaxWaitMilSecond):
        bLooping = True
        maxWaitTimeCheck = CSkBot_Common_Share.CSkBot_MilTimeIdentify(
            iMaxWaitMilSecond)

        while (bLooping):
            # 队列有内容?
            if (len(self.s_listPostMsgStore) == 0):
                bLooping = False
            else:
                # 时间超时?
                if (maxWaitTimeCheck.CheckTimeDiff()):
                    bLooping = False
                else:
                    # 此处对每个单元进行执行Send
                    nextMsgPostUnit = self.s_listPostMsgStore.pop(0)
                    if (nextMsgPostUnit.s_lDestTaskUID):
                        self.ExecTaskUnitMessage_UID(
                            fromSenderUnit=nextMsgPostUnit.s_senderRequestUnit,
                            lTaskUID=nextMsgPostUnit.s_lDestTaskUID,
                            messageID=nextMsgPostUnit.s_lMessageID,
                            bSendOrPost=True,
                            strMsgJsonParam=nextMsgPostUnit.s_strMsgJsonParam,
                            strMsgParam=nextMsgPostUnit.s_strMsgParam,
                            lMsgParam1=nextMsgPostUnit.s_lMsgParam1,
                            lMsgParam2=nextMsgPostUnit.s_lMsgParam2)

                    elif (nextMsgPostUnit.s_strDestTaskFullName):
                        if (nextMsgPostUnit.s_strTaskParam):
                            self.ExecTaskUnitMessage_StrParam(
                                fromSenderUnit=nextMsgPostUnit.
                                s_senderRequestUnit,
                                strFbUnitFullName=nextMsgPostUnit.
                                s_strDestTaskFullName,
                                strDestTaskParam=nextMsgPostUnit.
                                s_strTaskParam,
                                messageID=nextMsgPostUnit.s_lMessageID,
                                bSendOrPost=True,
                                strMsgJsonParam=nextMsgPostUnit.
                                s_strMsgJsonParam,
                                strMsgParam=nextMsgPostUnit.s_strMsgParam,
                                lMsgParam1=nextMsgPostUnit.s_lMsgParam1,
                                lMsgParam2=nextMsgPostUnit.s_lMsgParam2)
                        else:
                            self.ExecTaskUnitMessage_LongParam(
                                fromSenderUnit=nextMsgPostUnit.
                                s_senderRequestUnit,
                                strFbUnitFullName=nextMsgPostUnit.
                                s_strDestTaskFullName,
                                lTaskParam1=nextMsgPostUnit.s_lTaskParam1,
                                lTaskParam2=nextMsgPostUnit.s_lTaskParam2,
                                messageID=nextMsgPostUnit.s_lMessageID,
                                bSendOrPost=True,
                                strMsgJsonParam=nextMsgPostUnit.
                                s_strMsgJsonParam,
                                strMsgParam=nextMsgPostUnit.s_strMsgParam,
                                lMsgParam1=nextMsgPostUnit.s_lMsgParam1,
                                lMsgParam2=nextMsgPostUnit.s_lMsgParam2)

                    pass
Exemplo n.º 3
0
    def __init__(self, strSelfClientID):
        self.strMyFullName = strSelfClientID + Exec_API_CallFiber.s_g_strMySubFixFiberName
        FiberUnit_Base.__init__(self, self.strMyFullName, bExRemoteCallMe=True)
        self.s_TimerSend = CSkBot_Common_Share.CSkBot_TimeIdentify(10)

        self.s_tobe_RunExecMsg = {
        }  # 等待调度运行的,远端执行消息. key=msgID, 内容=CUTRC_NATs_Comu_RunMessageUnit。
        self.s_finish_RunExecMsg = {
        }  #已经完成的执行消息. key=msgID, 内容=完成返回结果 FiberMsgRet
        self.s_runningMsgID = ""
        self.s_runningRemoteExecMsgID = ""
        self.s_runningMsgContent = None  # 正在运行的消息内容
Exemplo n.º 4
0
    def ExecFiberCmd(self,
                     strDestFiberName,
                     messageID,
                     strDestFiberParam="",
                     lDestFiberParam1=0,
                     lDestFiberParm2=0,
                     strMsgJsonParam="",
                     strMsgParam="",
                     lMsgParam1=0,
                     lMsgParam2=0):

        strExecMsgUID = g_runTinyBrainCore.ExecAPICallFiber.RequestExecCmd(
            strDestFiberName,
            messageID,
            strDestFiberParam=strDestFiberParam,
            lDestFiberParam1=lDestFiberParam1,
            lDestFiberParm2=lDestFiberParm2,
            strMsgJsonParam=strMsgJsonParam,
            strMsgParam=strMsgParam,
            lMsgParam1=lMsgParam1,
            lMsgParam2=lMsgParam2)

        failTimeCheck = CSkBot_Common_Share.CSkBot_TimeIdentify(30)
        # 循环查询
        while (Global_TinyBrainCore.IsGlobalRunning()):
            if (failTimeCheck.CheckTimeDiff()):
                # 是否超时?
                break
            g_runTinyBrainCore.TimerCheck()

            bTaskFinish, execCmdRet = g_runTinyBrainCore.ExecAPICallFiber.QueryExecCmdFinish(
                strExecMsgUID)
            if (bTaskFinish):
                return execCmdRet

        # 取消
        g_runTinyBrainCore.ExecAPICallFiber.CancelExecCmd(strExecMsgUID)
        # 此处返回失败
        failMsgRet = FiberMsgRet(FiberMsgRet.s_g_iExecResult_Fail)
        return failMsgRet
Exemplo n.º 5
0
    def __init__(self):
        self.s_dictUIDFiberTasks = {
        }  # 唯一ID对应的任务字典。 key=long, 内容=FiberUnit_Base
        self.s_dictStrParamFiberTasks = {
        }  # 字符串纤程任务字典. key=string, 内容=FiberUnit_Base
        self.s_dictLongLongParamFiberTasks = {
        }  # 长参数任务字典. key=string, 内容=FiberUnit_Base

        self.s_listPostMsgStore = []  # postMessge的队列

        self.s_iMaxContinueFreeCount = 10  # 空闲10次就大休眠一次
        self.s_iLastContinueFreeCount = 0  # 上次连续休眠的时间
        self.s_iFreeIdleMilSecond = 1000  # 休眠的等待时间
        self.s_bNeedSleep = False

        self.s_listFiberTaskUnit = []  # 任务单元队列
        self.s_lastFreeMilTimeDiff = CSkBot_Common_Share.CSkBot_MilTimeIdentify(
            self.s_iFreeIdleMilSecond)  # 空闲检查

        self.s_store_listNeedSendRemoteMsgTask = [
        ]  # 存储需要发送到远程任务 CUTRC_NATs_Comu_RunMessageUnit
        self.s_bUnitDictModified = False  # 单元字典有否改变?

        pass
Exemplo n.º 6
0
    strNLKAnalyerName="lessnet.FiberExec.NLKer"
    lMsgID_NLKer_SenterToWord=100001  # strParam文字内容, 回复:strValue分词json

    def __init__(self):
        PyExCallAPI_Base.__init__(self)
        pass

    def ExecNLK(self, strInput):
        execCmd=self.ExecFiberCmd( strDestFiberName=self.strNLKAnalyerName, messageID=self.lMsgID_NLKer_SenterToWord,
                                   strMsgParam=strInput)
        return execCmd.s_strRetValue


if __name__ == "__main__":
    Global_TinyBrainCore.InitBrainCore()

    stringArray=["这是一个测试单词句子", "中国的GDP正在持续增长", "今天太阳高照明天马路繁忙"]
    pyAPIExec = pyAPI_NLK()

    timerSend = CSkBot_Common_Share.CSkBot_TimeIdentify()

    while(Global_TinyBrainCore.IsGlobalRunning()):
        if(timerSend.CheckTimeDiff()):
            for eachStr in stringArray:
                strValue=pyAPIExec.ExecNLK(eachStr)
                CTYLB_Log.ShowLog(0, "result", strValue)
                pass

        time.sleep(0.1)
        Global_TinyBrainCore.TimerCheck()
Exemplo n.º 7
0
 def __init__(self):
     super(AIOSWebOSFiber, self).__init__(AIOSWebOSFiber.FiberName,
                                          bExRemoteCallMe=True)
     self.sendTimeIdentify = CSkBot_Common_Share.CSkBot_TimeIdentify(
         10)  #10秒发送超时检测
Exemplo n.º 8
0
 def Run():
     timeIdentify = CSkBot_Common_Share.CSkBot_TimeIdentify()
     utrcClinet = TYFiberBot_Mang_NATS_UTRClient_Instance()
     utrcClinet.AddFiberUnit(AIOSWebOSFiber())
     utrcClinet.Run()
Exemplo n.º 9
0
    def TimerCheck(self):
        bRetTaskBusy = False  # 返回状态值
        bExecTask, bExecTimerCheck = False, False  # 执行任务,执行时间检查

        # 是否为N毫秒检查一次?
        if (self.s_iCheckEveryMilSecond):
            if (self.s_lastFreeMilTimeDiff.CheckTimeDiff()):
                bExecTimerCheck = True
        else:
            bExecTimerCheck = True

        if (bExecTimerCheck):
            #上次繁忙吗?这次直接执行
            if (self.s_bLastBusyStatus):
                bExecTask = True
            else:
                # 上次还在进行持续空闲检查?
                if (self.s_bInContinueCheckFreeStatus):
                    if (self.s_lastContinueIdle_MilTimeDiff.CheckTimeDiff()):
                        bExecTask = True
                else:
                    bExecTask = True

            if (bExecTask):
                # 执行,时间检查
                startTimeDiffCheck = CSkBot_Common_Share.CSkBot_MilTimeIdentify(
                )

                try:
                    bRetTaskBusy = self.v_Base_TimerCheck()
                except Exception as e:
                    CTYLB_MainSys_MiscFunc.ShowExceptionInfo(e)
                    bRetTaskBusy = True

                curTimeDiff = startTimeDiffCheck.GetStartTimeDiff()
                iTotalMilSecond = curTimeDiff.microseconds
                if (iTotalMilSecond > 3000):
                    strFullParam="UID:%d, strParam:%s, lParam:%d-%d" % \
                                 (self.s_lUniqueID, self.s_strTaskParam, self.s_lTaskParam1, self.s_lTaskParam2)
                    CTYLB_Log.ShowLog(0, self.s_strFullTaskName,
                                      ("time:%d, " %
                                       (iTotalMilSecond / 1000)) +
                                      strFullParam)

                if (bRetTaskBusy):
                    # 如果繁忙,清空所有状态
                    self.s_bInContinueCheckFreeStatus = False
                    self.s_iKeepIdleCheckFitCount = 0
                else:
                    # 如果上次也为空闲
                    if (self.s_bLastBusyStatus):
                        self.s_iKeepIdleCheckFitCount += 1
                        if (self.s_iKeepIdleCheckFitCount >=
                                self.s_iMaxKeepIdleCheckCount):
                            # 持续空闲,进行空闲持续状态
                            self.s_bInContinueCheckFreeStatus = True
                            self.s_lastContinueIdle_MilTimeDiff.ResetToNow()

                self.s_bLastBusyStatus = bRetTaskBusy

        return bRetTaskBusy
Exemplo n.º 10
0
    def __init__(self, config_file="config/config.yaml", funcCallBack=None):
        TYFiberBot_Mang_NATS_Instance_Base.__init__(self, config_file,
                                                    funcCallBack)

        # 如果有更新,2秒发一次。否则,半分钟发一次 我的单元列表
        self.s_TimerCheck = CSkBot_Common_Share.CSkBot_TimeIdentify(2)
        self.s_AlwaysSendFbUnitList_TimerCheck = CSkBot_Common_Share.CSkBot_TimeIdentify(
            30)
        self.s_FBotSendFbUnitList_TimerCheck = CSkBot_Common_Share.CSkBot_TimeIdentify(
            30)

        CLessTYBotFrameThread.SetDefaultConsoleCompatible(
            FBotV4.environment_event_handle)

        # 初始化,加载FBot的配置
        strFBotUTRC_Server = CTylb_Bot_Exec_Frame.ReadIniSectionValue(
            config_file, Share_UTRC_CfgDef.g_str_section_FiberUTRC,
            Share_UTRC_CfgDef.g_str_key_FBot_UTRC_Server, "")
        self.s_utrcFBot = None

        self.s_toBeSend_FBot_PackageArray = []  # 等到发送给伙伴FBot的数据包队列
        self.s_timeDiff_SendInit = CSkBot_Common_Share.CSkBot_TimeIdentify(1)

        if (strFBotUTRC_Server):
            loopCallBack = []
            if (self.s_funcTimerCallBack):
                loopCallBack.append((self.s_funcTimerCallBack))
            loopCallBack.append(self.LoopEventCallBack)

            connectUTRCFBotSrvAddr = {
                strFBotUTRC_Server:
                [Share_UTRC_CfgDef.g_iFbUTRC_AcceptExecClient_ListenPort]
            }
            self.s_utrcFBot = FBotV4(connect_to_socks=connectUTRCFBotSrvAddr,
                                     loop_event_callbacks=loopCallBack,
                                     config_file=config_file)

            self.s_utrcFBot.add_connect_to_callbacks(
                sock_id=strFBotUTRC_Server,
                sock_port=Share_UTRC_CfgDef.
                g_iFbUTRC_AcceptExecClient_ListenPort,
                cmd_id=Share_UTRC_CfgDef.g_iFBot_Cmd_Nop_SendEcho_To_Srv,
                callback=self.OnFBot_InitCall_LoopSendToSrv,
                is_auto_callback=True)

            self.s_utrcFBot.add_connect_to_callbacks(
                sock_id=strFBotUTRC_Server,
                sock_port=Share_UTRC_CfgDef.
                g_iFbUTRC_AcceptExecClient_ListenPort,
                cmd_id=Share_UTRC_CfgDef.g_iFBot_Cmd_NeedExecCmd_To_Client,
                callback=self.OnFBot_Client_Cmd_NeedExecCmd,
                is_auto_callback=False)

            self.s_utrcFBot.add_connect_to_callbacks(
                sock_id=strFBotUTRC_Server,
                sock_port=Share_UTRC_CfgDef.
                g_iFbUTRC_AcceptExecClient_ListenPort,
                cmd_id=Share_UTRC_CfgDef.g_iFbUTRC_Cmd_Reply_Message_Result,
                callback=self.OnFBot_Client_Cmd_ReplyMessageResult,
                is_auto_callback=False)

        pass
Exemplo n.º 11
0
 def __init__(self):
     self.s_timeIdentify = CSkBot_Common_Share.CSkBot_TimeIdentify()
     self.s_utrcClient = TYFiberBot_Mang_NATS_UTRClient_Instance()
Exemplo n.º 12
0
 def __init__(self):
     FiberUnit_Base.__init__(self,
                             self.s_g_strMyFiberName,
                             bExRemoteCallMe=True)
     self.s_TimerSend = CSkBot_Common_Share.CSkBot_TimeIdentify(8)
     self.s_dict_msgResult = {}  # 消息结果
Exemplo n.º 13
0
    def __init__(self, config_file="config/config.yaml", funcCallBack=None):
        self.s_collect_FbNameDict_Mang = Collect_FbNameDict_Mang()  # 集合纤程单元的管理


        # 临时存储处理FBot-客户端的Fb单元列表 字典
        self.s_recvFBotClientFibersDicts={} # 接收到FBot客户端 纤程名字典. key=FBot客户端名字, 内容=CUTRC_NATs_ComuFiberList

        TYFiberBot_Mang_UTRC_NATS_Instance.__init__(self, config_file, funcCallBack)
        # 保存本地变了

        iFbUTRC_ListenPeer_Port = Share_UTRC_CfgDef.g_iFbUTRC_ListenPeer_Port  # 纤程TRC服务器的监听端口
        # 线程UTRC服务器,对FBot客户端的监听端口
        iFbUTRC_AcceptExecClient_ListenPort=Share_UTRC_CfgDef.g_iFbUTRC_AcceptExecClient_ListenPort

        connectSrvAddr={}
        strPeerFbUTCServerAddr = CTylb_Bot_Exec_Frame.ReadIniSectionValue(config_file, Share_UTRC_CfgDef.g_str_section_FiberUTRC,
                                                                           Share_UTRC_CfgDef.g_str_key_FbUTRC_FBot_PeerServerAddr, "")
        if(strPeerFbUTCServerAddr):
            connectSrvAddr[strPeerFbUTCServerAddr] = [iFbUTRC_ListenPeer_Port]

        # 创建FBot服务器
        loopCallBack=[]
        if(self.s_funcTimerCallBack):
            loopCallBack=[self.s_funcTimerCallBack]

        loopCallBack.append(self.LoopEventCallBack)
        self.s_RunFBotV4=FBotV4(listen_socks=[iFbUTRC_ListenPeer_Port, iFbUTRC_AcceptExecClient_ListenPort],
                                listen_accpet_callback=self.FBot_Listen_Accept_Callback,
                                config_file=config_file, loop_event_callbacks=loopCallBack,
                                connect_to_socks=connectSrvAddr)

        # 针对 PeerSrv的消息处理
        self.s_RunFBotV4.add_listen_callbacks( Share_UTRC_CfgDef.g_iPeerFbUTRC_Cmd_BroadCast_Peer_FbUnitNames,
                                               self.OnListen_Recv_Peer_BroadCastFbUnitName)
        self.s_RunFBotV4.add_listen_callbacks(Share_UTRC_CfgDef.g_iPeerFbUTRC_Cmd_NeedPeerFBot_Run_Message,
                                              self.OnListen_Recv_Peer_ExecRunMessage)

        # 针对 FBot-ExecClient - g_iFbUTRC_AcceptExecClient_ListenPort 的消息处理. client发到utrc服务端的
        self.s_RunFBotV4.add_listen_callbacks(Share_UTRC_CfgDef.g_iFBot_Cmd_Nop_SendEcho_To_Srv,
                                              self.OnListen_Recv_Client_Nop_SendEcho)
        self.s_RunFBotV4.add_listen_callbacks(Share_UTRC_CfgDef.g_iFbot_Cmd_TransExecMsg_To_Srv,
                                              self.OnListen_Recv_Client_TransExecMsg)
        self.s_RunFBotV4.add_listen_callbacks(Share_UTRC_CfgDef.g_iFBot_Cmd_Report_FbUnitList_To_Srv,
                                              self.OnListen_Recv_Client_Report_FbUnitList)
        self.s_RunFBotV4.add_listen_callbacks(Share_UTRC_CfgDef.g_iFBot_Cmd_Reply_ExecMsg_To_Srv,
                                              self.OnListen_Recv_Client_Reply_execMsg)


        if(strPeerFbUTCServerAddr):
            self.s_RunFBotV4.add_connect_to_callbacks(
                sock_id=strPeerFbUTCServerAddr, sock_port=Share_UTRC_CfgDef.g_iFbUTRC_ListenPeer_Port,
                cmd_id=Share_UTRC_CfgDef.g_iPeerFbUTRC_Cmd_BroadCast_Peer_FbUnitNames,
                callback=self.OnFBot_PeerSrv_BroadCastPeerFbUnitName, is_auto_callback=True)

        # 初始化Fiber管理
        # 对信息传递 以纤程单元实现。收到nats服务器信息,判断是否在本地队列。不在,则发送给tybot
        self.s_TimerDiff_PeerFBotSrv_FbUnit_Notify = CSkBot_Common_Share.CSkBot_TimeIdentify(30)
        self.s_toBeSend_PeerFBot_FbNames_Package = None  # 等待发送的广播名字数据包
        self.s_toBeSend_PeerFBot_PackageArray = []  # 等到发送给伙伴FBot的数据包队列
        self.s_toBeSend_FBotClient_PackageDict={}  # 等待发送给 客户端的 Package. key=名字, 内容=[]

        pass
Exemplo n.º 14
0
 def __init__(self):
     self.s_utrcClient = TYFiberBot_QueryUTRCClient_Instance(funcCallBack= self.funcCheckCallBack)
     self.s_TimerSend=CSkBot_Common_Share.CSkBot_TimeIdentify()
Exemplo n.º 15
0
 def __init__(self):
     FiberUnit_Base.__init__(self, self.s_g_strMyFiberName, bExRemoteCallMe=True)
     self.s_TimerSend=CSkBot_Common_Share.CSkBot_TimeIdentify()
     self.s_dict_AllUTRCClientInfo={}