在Python中同时运行三个函数不起作用

时间:2017-02-15 22:39:14

标签: android python shell

我正在尝试同时在几台Android设备上安装Android APK。

我的想法是调用安装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.

抛开事实,如果亚行能够同时安装apks ......我期望发生的事情类似于此:

[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.

1 个答案:

答案 0 :(得分:0)

您可以在Thread构造函数中使用args param来运行它而不是部分:

Thread(target=install, args=('emulator-5558',)).start()