0
我想同时在几个Android设备上同时安装Android APK。在Python中同时运行三个函数不起作用
我的想法是来调用安装APK像shell命令:它启动新的终端处理
adb -s DEVICE_NAME install APK &
随后与&
。
我的Python脚本应该同时执行install(device_name)
函数。但它没有发生。有一个代码:
from ShellHelper import *
from threading import Thread
import time
import datetime
shellHelper = ShellHelper()
apk_dir = "app-automation-integrationTest.apk"
def install(device_name):
start_time = int(round(time.time() * 1000))
print('[{:%H:%M:%S}]: '.format(datetime.datetime.now())
+ "Installation started on device '" + device_name + "'.")
install_cmd = "adb -s " + device_name + " install " + apk_dir + " &"
shellHelper.execute_shell(install_cmd)
end_time = int(round(time.time() * 1000))
print('[{:%H:%M:%S}]: '.format(datetime.datetime.now())
+ "Installation ended on device '" + device_name + "'. It took: " + str(
(end_time - start_time)/1000) + " seconds.")
if __name__ == '__main__':
Thread(target=install("emulator-5554")).start()
Thread(target=install("emulator-5556")).start()
Thread(target=install("emulator-5558")).start()
和日志:
[23:31:50]: Installation started on device 'emulator-5554'.
[23:32:33]: Installation ended on device 'emulator-5554'. It took: 42.671 seconds.
[23:32:33]: Installation started on device 'emulator-5556'.
[23:32:37]: Installation ended on device 'emulator-5556'. It took: 4.451 seconds.
[23:32:37]: Installation started on device 'emulator-5558'.
[23:32:46]: Installation ended on device 'emulator-5558'. It took: 8.98 seconds.
扔一边事实上,如果亚行能够在同一时间安装的APK的......我期望出现的情况是类似这样的东西:
[23:31:50]: Installation started on device 'emulator-5554'.
[23:31:50]: Installation started on device 'emulator-5556'.
[23:31:50]: Installation started on device 'emulator-5558'.
[23:32:10]: Installation ended on device 'emulator-5554'. It took: 20.00 seconds.
[23:32:30]: Installation ended on device 'emulator-5558'. It took: 40.00 seconds.
[23:32:33]: Installation ended on device 'emulator-5556'. It took: 43.00 seconds.
我在做什么错?
//这个工作
Thread(target=partial(install, "emulator-5554")).start()
Thread(target=partial(install, "emulator-5556")).start()
Thread(target=partial(install, "emulator-5558")).start()
和预期结果:
[01:07:44]: Installation started on device 'emulator-5554'.
[01:07:44]: Installation started on device 'emulator-5556'.
[01:07:44]: Installation started on device 'emulator-5558'.
[01:08:00]: Installation ended on device 'emulator-5558'. It took: 15.303 seconds.
[01:08:00]: Installation ended on device 'emulator-5556'. It took: 15.571 seconds.
[01:08:01]: Installation ended on device 'emulator-5554'. It took: 16.748 seconds.
为什么'安装'不可调用?这是一个功能.. –
您需要传递对象,而不是通话。您可以使用线程的'args'构造函数属性指定函数的参数。部分不需要 –
谢谢你解释。我怎样才能运行它,而不使用部分?你能举个例子吗? :) – F1sher