关闭

2016年6月2日 几乎是强迫自己在更新 关于python的多线程处理

标签: python多线程需求
242人阅读 评论(0) 收藏 举报
分类:

老生常谈的东西

这是我这段代码的第21次更新了,依然不能实现全部的设计需求

然而都这个点儿了,先睡吧

欠下的债,早晚都要还的

try:
    import sys
    import time
    import os
    import platform
    import threading
    #os.chdir('../res')
    #sys.path.append(os.getcwd())
    #from adb import ADB

    #except ImportError as e:
    #   print("[f] Required module missing. %s" % e.args[0])
    #   sys.exit(-1)
except:
        pass

class StatusMonitor:
    def __init__(self):
        self.adb_task = None
        self._dict = {}
        try:
            base_dir = os.path.dirname(os.getcwd()) #Get the abs path for the config file
            configfile_path = os.path.join(base_dir, r'\untility\configfile.txt') #Get configfile.txt
            CONFIGFILE = open(configfile_path, 'r')
            adbstatus_path = os.path.join(base_dir, r'\logs\adb_status.txt')
            self.ADBFILE = open(adbstatus_path, 'a+')
        except:
            print ("File not exist")
            return False
        #Create a dict for the config file
        for line in CONFIGFILE:
            (key,value) = line.strip().split(':',maxsplit=1)
            self._dict[key] = value
        global adb_status_monitor
        self.adb_status_monitor = 1



    def device_state_monitor_start(self):
        #status = self.multiprocessing.Value('b', 1)
        #print ("Sub process for ADB_status monitor started ",status)
        #adb_task = multiprocessing.Process(target=device_state_monitor_start)
        #adb_task.start()
        #Check adb status
        while True:
            if self.adb_status_monitor == 1:
                Time_mark = time.strftime("%Y-%m-%d %H:%M:%S")
                self.ADBFILE.write(str(Time_mark + '  '))
                #adb_status = self._ABDInstance.shell_command('ping -c 5 localhost')
                adb_status = True
                if adb_status == True:
                    self.ADBFILE.write('ADB works normally')
                    self.ADBFILE.write('\n')
                    print ('ADB works normally')
                        #While ADB works normally, then check the Moneky process
                        #monkey_result = self.shell_command( "\ps|grep com.android.commands.monkey\")
                    monkey_result = True
                    if monkey_result == True:
                        self.ADBFILE.write('Monkey works normally')
                        self.ADBFILE.write('\n')
                        print ('Monkey works normally')
                    else:
                        self.ADBFILE.write('Monkey process is terminated')
                        self.ADBFILE.write('\n')
                        print ('Monkey works abnormally')
                else:
                    self.ADBFILE.write('ADB is disconnected,and Monkey process can not be found')
                    self.ADBFILE.write('\n')
                    print ('ADB is disconnected')
                #wait 10 minutes
                time.sleep(8)
            else:
                break

    def device_state_monitor_stop(self):
        global adb_status_monitor
        self.adb_status_monitor = 0
        self.ADBFILE.close()

    def regNewProcess(self):
        self.adb_task = threading.Thread(target=self.device_state_monitor_start)
        #self.adb_task.setDaemon(True)
        self.adb_task.start()
        print('The thread adb task start') #打印这句话证明线程启动
    """def check_parameters(self):
        #Check the config file for blank value
        configfile = self._dict
        print (configfile)
        for key in configfile:    
            if configfile[key] =='':
                print ([key], "Error: Value is blank")
                return False
            else:
                #print ([key,configfile[key]],'Pass')
                pass
        test_hour = self.get_parameters('TEST_HOUR')
        cycle_time = self.get_parameters('CYCLE_TIME')
        change_prof_hour = self.get_parameters('CHANGE_PROFILE_HOUR')
        test_type = self.get_parameters('TEST_TYPE')
        execute_method = self.get_parameters('EXECUTE_METHOD')
        #Check the Test_hour/Cycle_time/change_prof_hour
        if int(test_hour) < int(cycle_time):
            # 'TEST_HOUR' should longer than 'CYCLE_TIME'
            print ("Error: test_hour < cycle time is incorrect")
            return False
        elif int(cycle_time) < int(change_prof_hour):
            # 'CYCLE_TIME' should longer than 'CHANGE_PROFILE_TIME'
            print ("Error: cycle_time < change_prof_hour is incorrect")
            return False
        elif (test_type) != 'black'and (test_type) != 'white':
            # Only 'black' or 'white' is available in 'TEST_TYPE'
            print ("Error: test_type should be 'black' or 'white'.")
            return False
        elif (execute_method) != '1' and (execute_method) != '2':
            # Only '1' or '2' is available in 'EXECUTE_METHOD'
            print ("Error: execute_method should be '1' or '2'")
            return False
        else:
            print ("Check parameters completed.No error occurs")
        return True"""

    def get_parameters(self,item):
        value = self._dict.get(item,None)
        if value == None:
            print (item,' KEY error, please check the key_name')
        return value      
# Test module    
if __name__=='__main__':
    i = StatusMonitor()
    k = i.regNewProcess()
    #l = i.device_state_monitor_stop()
    mm = i.get_parameters('TEST_TYPE')
    print(k,mm)





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1936次
    • 积分:94
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章存档