zoukankan      html  css  js  c++  java
  • appium-7-多设备并发

     

     
    
    

    多设备并发

    在日常使用,只有一台设备进行自动化脚本运行,往往比较耗时,我们更多的希望更多的手机,同时运行不同的模块,所以就需要用到线程,我们先写一个线程的代码:

    复制代码
     
    def task(desired_cap,udid,link):
    driver = webdriver.Remote('http://127.0.0.1:%s/wd/hub' % 4723, desired_cap)
    driver.implicitly_wait(10)
    xxxxxxxxxx#上面两行代码是启动所有的设备,启动完成后,执行封装的方法
    def run_th():
      udid=['xxx','xxx','xxx']#各个设备的uid
      for u in range(len(udid)):
      devices_udid = {"udid": "{}".format(xx), "deviceName": "xiaomi-redmi_6-{}".format(udid[u])}
      desired_cap = dict(devices_udid, **desired_caps)
      t1 = threading.Thread(target=task, args=(desired_cap,udid[u],dingdings))#添加线程
      t1.setName(udid[u])#设置线程名,把设备id设置成线程名,可以根据自己的需要
      udids.append({"name":udid[u],"params":"{}".format(desired_cap)})
      threads.append(t1)
      for t in threads:
      t.start()#启动线程
      initThreadsName.append(t.getName())
      time.sleep(5)
       checkThread(180)#调用线程监控,代码在下面
    复制代码

    很多的时候,我们的线程可以运行了,但是在运行的过程中,经常会设备闪退,或者没有找到元素被强制退出了,所以我们要对我们的线程添加监控,保证线程报错之后,能够立马重启

    复制代码
     
    def _async_raise(tid, exctype):#监控线程
    """raises the exception, performs cleanup if needed"""
    tid = ctypes.c_long(tid)
    if not inspect.isclass(exctype):
    exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
    if res == 0:
    raise ValueError("invalid thread id")
    elif res != 1:
    # """if it returns a number greater than one, you're in trouble,
    # and you should call it again with exc=NULL to revert the effect"""
    ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
    raise SystemError("PyThreadState_SetAsyncExc failed")


    def stop_thread(threadid):#停止线程
    _async_raise(threadid, SystemExit)
    def checkThread(sleeptimes=180,initThreadsName=[],link=''):
    while True:#循环运行
    nowThreadsName=[]#用来保存当前线程名称
    now=threading.enumerate()#获取当前线程名
    nowpids=[]
    for i in now:
    nowThreadsName.append(i.getName())#保存当前线程名称
    nowpid={"name":i.getName(),"pid":i.ident}
    nowpids.append(nowpid)#报存当前线程id
    for udid in initThreadsName:
    if udid in nowThreadsName:
    print("正常---"+udid)
    time.sleep(10)
    # pass #当前某线程名包含在初始化线程组中,可以认为线程仍在运行
    else:
    print ('{}==='.format(time.strftime('%m_%d:%H:%M'))+udid+':stopped,now restart')
    uds=[u for u in udids if udid == u['name']]
    if uds:
    t=threading.Thread(target=task2,args=(eval(uds[0]['params']),udid,link))#重启线程
    t.setName(udid)#重设name
    t.start()
    time.sleep(240)
    break
    time.sleep(sleeptimes)#隔一段时间重新运行,检测有没有线程down
    复制代码
    时间有限,没有那么多时间写博客,大家进群分享
  • 相关阅读:
    学习python -- 第018天 os模块
    学习python -- 第017天 文件读写
    重看算法 -- 动态规划 最大子段和问题
    重看算法 -- 递归与分治
    学习python -- 第016天 模块
    学习python -- 第016天 浅拷贝和深拷贝
    网络字节序、主机字节序(摘抄)
    C++/C常量
    结构化程序设计
    循环(高质量4.10)
  • 原文地址:https://www.cnblogs.com/SparkProgram/p/14441849.html
Copyright © 2011-2022 走看看