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

F1sher

我试图同时在几个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.

如果ADB能够同时安装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.
托马斯·莫德钦斯基(TomaszModrzyński)

您可以通过args在Thread构造函数中使用param来运行它,而无需局部运行

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从批处理文件同时运行两个 python 脚本,使用 anaconda,不起作用

在Python中同时运行两个函数

C中的第二个和第三个gets()函数不起作用

在 Python 中同时运行函数

三个下拉事件更改不起作用

htaccess第三个参数不起作用

MongoDB聚合中的第三个条件不起作用

熊猫中的外部联接三个数据框不起作用

为什么我的第三个条件在搜索中不起作用?

如何在while循环中以不同的睡眠时间同时运行三个线程?

通过信号量执行线程同步后,无法同时运行三个线程

在python中合并2个Linkedlists不起作用。创建的第三个链表在执行后给我一个NULL值/结果

如何让两个函数与python同时运行?

实现 C++ 程序,允许用户在后台运行多个进程(最多同时运行三个)

同时运行两个命令并通过管道传输到第三个命令

当更新同时运行时,乐观锁定不起作用 Spring Data JPA

如何在python中同时运行2个列表?

在 Selenium Python 中同时运行两个用户

Python - 同时运行多个异步函数

2个代码有效,第三个不起作用

Excel IF(OR)-两个OR公式有效,但三个不起作用?

在Java(Eclipse)中同时运行函数

DisplayPort 1.2菊花链第三个监视器不起作用

向 SELECT 添加第三个值时 SQL 连接不起作用

向ngClass添加第三个条件不起作用

三个Js简单着色器不起作用

SQL查询在第三个AND附近不起作用

在三个js球上的样条曲线不起作用

高图,三个输入之一不起作用