1. 程式人生 > 實用技巧 >Python多執行緒的使用,threading.Thread(target=monkeyCmd())啟動一個執行緒後,程式碼不繼續向後執行

Python多執行緒的使用,threading.Thread(target=monkeyCmd())啟動一個執行緒後,程式碼不繼續向後執行

唉 !我還以為是執行緒排程的問題呢,結果竟然是執行命令列的呼叫方法有問題

def monkeyCmd():
    os.system("adb shell monkey " + '-p ' + package_name + " -v -v -v -s " + str(
        seed) + " --ignore-crashes --ignore-timeouts --ignore-security-exceptions --kill-process-after-error --pct-trackball 0 --pct-nav 0 --pct-anyevent 0 --pct-flip 0 --pct-pinchzoom 0 --pct-syskeys 0 --throttle 300 " + test_times + " > monkeyTest.txt")

#   啟動一個執行緒執行Money命令
def monkeyRun():
    threading.Thread(target=monkeyCmd()) #啟動一個執行money 命令
    print('啟動一個執行緒開始monkey')

  

threading.Thread(target=monkeyCmd()) 啟動一個Monkey命令後,程式碼沒有繼續向後執行,而是等待adb shell命令列中的內容執行完畢再繼續

解決辦法:

os.system("adb shell monkey " + '-p ' + package_name + " -v -v -v -s " + str(
seed) + " --ignore-crashes --ignore-timeouts --ignore-security-exceptions --kill-process-after-error --pct-trackball 0 --pct-nav 0 --pct-anyevent 0 --pct-flip 0 --pct-pinchzoom 0 --pct-syskeys 0 --throttle 300 " + test_times + " > monkeyTest.txt")

修改為

os.popen("adb shell monkey " + '-p ' + package_name + " -v -v -v -s " + str(
seed) + " --ignore-crashes --ignore-timeouts --ignore-security-exceptions --kill-process-after-error --pct-trackball 0 --pct-nav 0 --pct-anyevent 0 --pct-flip 0 --pct-pinchzoom 0 --pct-syskeys 0 --throttle 300 " + test_times + " > monkeyTest.txt")
修改後程式碼能繼續向下執行而不用等待 adb shell命令執行結束後再往下執行