简体   繁体   English

杀死在python中用subprocess.popen打开的adb

[英]kill adb opened with subprocess.popen in python

I need to run a command adb get-state from my python library. 我需要从我的python库运行命令adb get-state。

I used 我用了

subprocess.popen

to execute the command and killed the process using pid. 执行命令并使用pid终止进程。

If the adb daemon is not in running state, adb process will create an extra adb process and terminate the first process created after starting the daemon (even manually its the same). 如果adb守护程序未处于运行状态,则adb进程将创建一个额外的adb进程,并终止启动该守护程序后创建的第一个进程(甚至手动启动该进程)。

Hence I am not able to kill the adb process opened by subprocess open since it has the pid of the first adb process (which has already terminated) 因此,我无法杀死由子进程打开打开的adb进程,因为它具有第一个adb进程的pid(已经终止)

Is there any way I can kill the real adb process which executes the command from python. 有什么办法可以杀死真正的从python执行命令的adb进程。

I will suggest you yo kill all the running processes of adb before starting a new one . 我建议您在开始一个新的进程之前先杀死adb的所有正在运行的进程。 Here is some sample code that can kill already running process under windows environment. 这是一些示例代码,它们可以杀死Windows环境下已经运行的进程。

This code Kills any adb process that is already running and then opens a new process daemon for adb 此代码将杀死任何已在运行的adb进程,然后为adb打开一个新的进程守护程序

Sample Code : 样例代码:

import subprocess
import os
p = subprocess.Popen("tasklist", stdout=subprocess.PIPE)

RunningProcessLIst = p.stdout.readlines()
print("Kill all running instances of adb.exe")
for runningPro in RunningProcessLIst :
    if "adb.exe" in str(runningPro) :

        os.system("taskkill /F /IM adb.exe")




process =subprocess.Popen("I:\\adb\\Win32\\adb.exe adb get-state") # you subprocesses call will be here

Hope this helps 希望这可以帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM