python installs apk packages concurrently for multiple android devices

Keywords: Python encoding emulator

I. demand

Recently, we need to install APK packages for several pads, but in the previous series scripts, it takes 1 minute for one pad to install an APK and 80 minutes for 80 pads to install an apk. So I wonder if I can let the task pool install the APK in batches in parallel.

Second, the realization of ideas:

Think of using multi-process method, even using python's pool.map method, to assign tasks to each task pool, to start multi-task pool parallel processing tasks, nonsense, direct code:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import time
from multiprocessing import Pool
list=[]


def getDevicesAll(): #Get the number and name of devices
    devices = []

    try:
        for dName_ in os.popen("adb devices"):
            if "\t" in dName_:
                if dName_.find("emulator") < 0:
                    devices.append(dName_.split("\t")[0])
        devices.sort(cmp=None, key=None, reverse=False)
        print(devices)
    except:
        pass

    print(u"\n Device name: %s \n Total quantity:%s platform" % (devices, len(devices)))

    return devices

def quickinstall(device):	#Unload the original apk
    try:
        os.system('adb -s ' + device + ' uninstall Package name')
        os.system('adb -s ' + device + ' uninstall Package name')
    except:
        print(device + "Uninstall Failed\n")

    print(device + "Uninstall successfully\n")

    try:
        for i in list:
            os.system('adb -s ' + device + ' install ' + i)
    except:
        print(device + "Installation failed\n")

    print(device + "Successful installation\n")
    


def qainstall(devices):
    starttime=time.time()
    pool = Pool(8)		#Create 8 task pools
    result=pool.map(quickinstall,devices)
    endtime=time.time()
    pool.close()
    pool.join()
    print(endtime-starttime)	#Printing time


if __name__ == "__main__":
     filesname = '/Users/User name/Desktop/package'		#Get the installation package
     for parent, dirnames, filnames in os.walk(filesname):
         for filname in filnames:
             path = os.path.join(parent, filname)
             list.append(path)
     try:
         devices = getDevicesAll()
     except:
         print("Error acquiring device")

     res = input("Input 1 to start updating:")
     if int(res) == 1:
         try:
             qainstall(devices)
         except:
             print("Update failed")
     Touch(devices)

 

Posted by Nairb on Sat, 23 Mar 2019 22:51:28 -0700