如何在不终止当前进程的情况下杀死窗口中的进程



你好,我只是不会在不杀死当前进程的情况下杀死一个进程,假设我有一个在txt文件中编写字符串的python代码,例如

import os
while True:
with open('input.txt' , 'r') as re:
all_data = re.read()
if all_data == 'pause':
os.system('kill.bat')
else:
print("nContinuen")

在这里它将读取输入.txt如果等于暂停,那么它将运行 kill.bat在这里我想重新启动此代码以执行此操作,我将编写另一个脚本 kill.bat重新启动此代码但问题是它没有重新启动,因为它正在杀死 kill.bat 文件但我不会只杀死 python 终端并重新启动它 我怎么能做到 这里是 kill.bat 文件代码

taskkill /IM cmd.exe
python main.py

main.py 是我的蟒蛇文件

不错的尝试@Pryanshu

你做对了。您当前方法的问题是python,批处理脚本将处于相同的进程下并同时被杀死。 所以只需稍微改变一下方法

要点如下:

get current process id
while loop 
read file
if matches condition
call kill.bat and pass this python process ID as separate process

get first parameter (which is the python process to kill)
kill the process via taskkill
start the python script as seprate process
exit

助手

get current process ID     - pid = os.getpid()
pass id to batch script    - os.system("kill.bat %s" % pid)
batch script get arguments - %1
this %1 will contain the first argument passed. 
by default %* will contain all the arguments passed to the script.
python start program as 
separate process           - use subprocess python package
cmd start program as 
separate process           - use Start command
kill process via taskkill  - taskkill /F /PID <pid>
replace <pid> with your argument

我知道你可以处理代码部分。您必须使用助手并将它们组合在一起来完成任务。莱姆知道输出。

如果您有任何问题,请发表评论。我会尽快回复

干杯

相关内容

最新更新