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

原创 2016年06月02日 00:35:04

老生常谈的东西

这是我这段代码的第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)





版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

windows下python subprocess.Popen执行adb 命令进程无法终止

logcmd = "adb logcat -v time > C:/log.txt" self.Popen = subprocess.Popen(logcmd ,stdout = subpr...

2012年08月2日--Java多线程管理

一、线程概述     线程是程序运行的基本执行单元。当操作系统(不包括单线程的操作系统,如微软早期的DOS)在执行一个程序时,会在系统中建立一个进程,而在这个进程中,必须至少建立一个线程(这个线程被...

新版微软一站式示例代码库 6月2日更新下载。

新版微软一站式示例代码库 6月2日更新下载。下载地址: http://1code.codeplex.com/releases/view/67076你也可以通过 示例代码浏览器 或 示例代码浏览器Vis...

2016年截止6月17日以来指引文档(Guide)的更新列表(Apple)

如题:2016年依赖指引(Guide)文档的更新列表(Apple),截止到6月17日

Python多线程爬虫

  • 2016-11-07 14:21
  • 33KB
  • 下载

Python模块学习:threading 多线程控制和处理

今天来学习Python中另一个操作线程的模块:threading。threading通过对thread模块进行二次封装,提供了更方便的API来操作线程。今天内容比较多,闲话少说,现在就开始切入正题! ...

python多线程开发

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)