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