简体   繁体   English

python如何保持一个线程执行直到其他线程完成

[英]python how to keep one thread executing till other threading finished

I hope to record app(eg.com.clov4r.android.nil) the CPU occupancy when I operate the app(eg.doing monkey test) and finish recording when I eixt the app(eg.finishing monkey test). 我希望在操作应用程序(例如,进行猴子测试)时记录应用程序(例如,com.clov4r.android.nil)的CPU使用率,并在完成应用程序(例如,完成猴子测试)时完成记录。 How to realise it with python? 如何用python实现呢?

Some codes: 一些代码:

packagename = 'com.clov4r.android.nil'
cmd1 = 'adb shell top -d 5 | grep com.clov4r.android.nil'
cmd2 = 'adb shell monkey -v -p com.clov4r.android.nil --throttle 500 --ignore-crashes --ignore-timeouts --ignore-security-exceptions --monitor-native-crashes -s 2345 100'
t1 = threading.Thread(target=subprocess.call(cmd1, stdout=open(r'123.txt', 'w'))) 
t2 = threading.Thread(target=subprocess.call(cmd2))

You can use Thread.join() : 您可以使用Thread.join()

import threading, time

def worker():
    time.sleep(5)

t = threading.Thread(target=worker)
t.start()
t.join()
print('finished')

Events are a good way to communicate between threads ( http://docs.python.org/2/library/threading.html#event-objects ). 事件是在线程之间进行通信的好方法( http://docs.python.org/2/library/threading.html#event-objects )。 However, the other problem you will have is that the top command will essentially run forever. 但是,您将遇到的另一个问题是top命令将永远运行。 I would do something like this: 我会做这样的事情:

def run_top(event, top_cmd):
    s = subprocess.Popen(top_cmd, stdout=open('123.txt', 'w'))
    event.wait()  # Wait until event is set, then kill subprocess
    s.kill()

def run_monkey(event, monkey_cmd):
    subprocess.call(monkey_cmd)
    event.set()  # Once we're finished set the event to tell the other thread to exit

event = threading.Event()
threading.Thread(target=run_top, args=(event, your_top_command)).start()
threading.Thread(target=run_monkey, args=(event, your_monkey_command)).start()

There might be a way to kill the thread as well but that's pretty ugly, this way is much more controlled. 也可能有一种杀死线程的方法,但这很丑陋,这种方法受控制得多。

I would also say run_monkey() doesn't need to be run in a thread, but not sure what other code you have that may require it. 我还要说run_monkey()不需要在线程中运行,但是不确定您拥有哪些其他代码可能需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何暂停服务并等待其他服务完成 - How to pause Service and wait till other service is finished Android线程 - 在执行所有其他线程之前执行一个线程 - Android Threading - Executing One thread before all others are executed 如何停止当前线程,直到其他线程完成执行? - How to stop the current thread till other thread complete its execution? 在Android上,如何继续运行线程直到被用户停止? - On Android, how to keep running a thread till it's stopped by the user? 一种方法完成执行后如何启动方法执行 - How strart method execution after one method finished executing Android-确定线程何时完成执行 - Android - determining when a thread has finished executing 如何等待线程直到另一个线程完成 - How to waits the thread till another thread completes 如何保持单选按钮处于选中状态,直到在Android中的单选组中选中了另一个单选按钮? - How to keep the radio button checked till its other radio button is checked in a radio group in android? 如何更改选定的SlidingMenu行的背景颜色并保持不变,直到选择了其他行? - How to change the background color of the selected SlidingMenu row and keep it the same till some other row is selected? 在flutter完成一个之后执行for循环 - Executing a for loop after one is finished in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM