python 多线程,多进程执行测试

来源:互联网 发布:马鞍山网络大学 编辑:程序博客网 时间:2024/06/04 19:43

多线程执行

import threading

import pushclient
import time
import log

IP="192.16.30.30"
blRndUser=False


class ThreadDemo(threading.Thread):


    def __init__(self,index,create_time):#线程构造函数


        threading.Thread.__init__(self)


        self.index = index


        self.create_time = create_time


    def run(self): #线程运行代码
        
        PassCount=0
        SpaceCount=0
        try:
            pushclient.initPush(IP,boolRndUser=blRndUser)
            print 'Thread %d : is beginning......' % (self.index)
            while 1:
                time.sleep(1) #休眠1秒
                
                rec=pushclient.recevice(self.index,PassCount)
                #rec=1
                if rec==1:
                    PassCount=PassCount+1
                    print "Thread    "+str(self.index)+ "    PassCount    "+str(PassCount)+"    SpaceCount    "+str(SpaceCount)
                else:
                    SpaceCount=SpaceCount+1
                #print "    PassCount    "+str(PassCount)+"    SpaceCount    "+str(SpaceCount)
        except Exception,e:
            print 'error    '+str(e)
            pushclient.endPush()
            #log.log("error "+str(e))
            #print 'PassCount=    %s,SpaceCount=    %s'%(str(PassCount),str(SpaceCount))
        
        
if __name__=="__main__":
    print "when msg was successed  to be recevied,the window will be see the 'passcount'"
    print "__________________________________________\n"
    threadNumber = int(raw_input("please input the thread No. == \n"))
    
    newIP = (raw_input("please input the IP(default:) == \n"))
    
    rndUser=(raw_input("please check the tag,if nothing default is '',otherwise rand from 5w.No \n"))
    
    if newIP=="":
        pass
    else:
        IP=newIP
        
    if rndUser=="":
        blRndUser=False
    else:
        blRndUser=True
        
    threads = []
    
    print "test is beginning....."
    
    
    try:
        for index in range(threadNumber):
            time.sleep(0.1)
            thread = ThreadDemo(index,time.time())
        
            thread.start() 
        
            threads.append(thread)
        
        for i in range(threadNumber):
            #time.sleep(1)
            time.sleep(0.1)
            threads[i].join()
    except Exception,e:
        log.log(str(e))

    


多进程执行:

import ctypes
from multiprocessing import Pool,freeze_support
import pushclient
import os
import time
import log
import json
import time


IP="172.16.30.30"
blRndUser=False


class MEMORYSTATUSEX(ctypes.Structure):
    _fields_ = [
        ("dwLength", ctypes.c_ulong),
        ("dwMemoryLoad", ctypes.c_ulong),
        ("ullTotalPhys", ctypes.c_ulonglong),
        ("ullAvailPhys", ctypes.c_ulonglong),
        ("ullTotalPageFile", ctypes.c_ulonglong),
        ("ullAvailPageFile", ctypes.c_ulonglong),
        ("ullTotalVirtual", ctypes.c_ulonglong),
        ("ullAvailVirtual", ctypes.c_ulonglong),
        ("sullAvailExtendedVirtual", ctypes.c_ulonglong),
    ]


    def __init__(self):
        # have to initialize this to the size of MEMORYSTATUSEX
        self.dwLength = ctypes.sizeof(self)
        super(MEMORYSTATUSEX, self).__init__()
        
def untilTime(wantnow):
    while True:
        now=time.strftime('%X',time.localtime(time.time()))
        
        
        if str(now)<wantnow:
            print "now=%s,set run time=%s,then sleep 60s." %(str(now),wantnow)
            time.sleep(60)
        else:
            print "now=%s,set run time=%s,running......" %(str(now),wantnow)
            break
        
        
        
def getConf():
    path=os.getcwd()
    path=path+"\\autotest.conf"
    rd=open(path )
    conf=rd.readline()
    return json.loads(conf)
        
def getTotalMEM():
    stat = MEMORYSTATUSEX()
    ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(stat))
    ullTotalPhys=stat.ullAvailPhys/(1024*1024)


    return ullTotalPhys


def runByProcess(x):
    time.sleep(0.5)
    print "pid=%s" % (os.getpid())
    print "pid=%s,starting init......" % (os.getpid())
    try:
        push(1)
    except Exception,e:
        print 'error    '+str(e)
        pushclient.endPush()
def push(items=1):
    PassCount=0
    SpaceCount=0
    
    pushclient.initPush(IP,boolRndUser=blRndUser)
    
    while(items<5):
        time.sleep(1)
        #print "pid=%s,get msg......" % (os.getpid())
        rec=pushclient.recevice(0,0)
                #rec=1
        if rec==1:
            PassCount=PassCount+1
            print "pid=%s,PassCount=%s,SpaceCount=%s"%(str(os.getpid()),str(PassCount),str(SpaceCount))
        else:
            SpaceCount=SpaceCount+1
        #print "pid=%s,PassCount=%s,SpaceCount=%s"%(str(os.getpid()),str(PassCount),str(SpaceCount))
        #log.log(str(os.getpid()))
        #items=items+1
    
if __name__ == '__main__':
    freeze_support()
    
    
    print "when msg was successed  to be recevied,the window will be see the 'passcount'"
    print "__________________________________________\n"
    
    strConf=getConf()
    if strConf["type"]=="0":
        if strConf["rnduser"]=="0":
            rndUser=False
        else:
            rndUser=True
            
        IP=strConf["ip"]
        wantnow=strConf["time"]
        untilTime(wantnow)
        totalMem=getTotalMEM()
    
        autoProcessNo=int(totalMem/12)
        
        processNumber=autoProcessNo
        print "process No.=%s,ip=%s,userdefault=%s" %(processNumber,IP,str(rndUser))
        #wait the time
    else:
        
        #print autoProcessNo
        
        processNumber = int(raw_input("1.please input the process No. == \n"))
        
        newIP = (raw_input("2.please input the IP(default:172.16.30.30) == \n"))
        
        rndUser=(raw_input("3.please check the tag,if nothing default is 'f9742e104c1c4211910ad9466c8f6a7d',otherwise rand from 5w.No \n"))
        
        if newIP=="":
            pass
        else:
            IP=newIP
            
        if rndUser=="":
            blRndUser=False
        else:
            blRndUser=True
        
    
    print "__________________________________________\n"
    time.sleep(1)
    print "test is beginning....."
    #time.sleep(1)
    print "__________________________________________\n"
    poolNumber=processNumber+1
    runNumber=poolNumber-1
    pool = Pool(processes=poolNumber)
    
    pool.map(runByProcess, range(runNumber))
    

0 0
原创粉丝点击