从另一个 python 脚本终止一个 python 脚本 [英] Terminate a python script from another python script

查看:77
本文介绍了从另一个 python 脚本终止一个 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长时间运行的 python 脚本,我希望能够从另一个 python 脚本结束.理想情况下,我正在寻找某种方式将进程 ID 设置为第一个脚本,并能够通过第二个脚本的 ID 查看它是否正在运行.此外,我希望能够终止那个长时间运行的进程.

I've got a long running python script that I want to be able to end from another python script. Ideally what I'm looking for is some way of setting a process ID to the first script and being able to see if it is running or not via that ID from the second. Additionally, I'd like to be able to terminate that long running process.

是否有任何很酷的捷径可以实现这一目标?

Any cool shortcuts exist to make this happen?

另外,我在 Windows 环境中工作.

Also, I'm working in a Windows environment.

我最近在这里找到了一个替代答案:检查是否python脚本正在运行

I just recently found an alternative answer here: Check to see if python script is running

推荐答案

您可以通过

import os
os.getpid()

并在 Unix 中杀死一个进程

and to kill a process in Unix

import os, signal
os.kill(5383, signal.SIGKILL)

在Windows中使用kill

to kill in Windows use

import subprocess as s
def killProcess(pid):
    s.Popen('taskkill /F /PID {0}'.format(pid), shell=True)

您可以将PID发送给其他程序,也可以在进程列表中搜索其他脚本的名称并使用上述脚本将其杀死.

You can send the PID to the other programm or you could search in the process-list to find the name of the other script and kill it with the above script.

希望能帮到你.

这篇关于从另一个 python 脚本终止一个 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆