当另一个 python 脚本正在运行时,如何停止我的 python 脚本? [英] How can I stop my python script when another python script is running?

查看:35
本文介绍了当另一个 python 脚本正在运行时,如何停止我的 python 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在当前的 Python 脚本已经运行时停止它.

I want to know how I can stop my current python script when it is already running.

明确地说,我想用我自己的python脚本编写它.所以首先,在 main 方法中,它会检查我的 python 脚本是否已经在运行.如果它正在运行,我想退出并显示错误消息.

To be clear, I want to write it in my own python script. So the very first thing, in main method, it will check if my python script is already running or not. If it is running, I want to exit out with an error message.

目前我正在尝试此代码:

Currently I am trying this code:

if os.system("ps ax | grep sample_python.py") == True:
     print "fail"
     exit(0)
else:
     print "pass"

上面的代码看起来像是在搜索python名称..但它总是进入else循环而不是进入if循环并退出..

The above code looks like it is grepping the python name.. but it always go to else loop instead of going into the if loop and exit..

因此,出于测试目的,我运行我的脚本,然后在另一个终端上再次运行我的脚本.第一个脚本不会停止,但第二个 python 不会进入 if 循环以打印错误消息.

So for testing purposes, I run my script and on another terminal I run my script again. First script doesn't stop but 2nd python doesn't go into the if loop to print out error message.

我该如何解决这个问题?

How can I fix this?

好的,我发现了一个愚蠢的东西,我想看看是否有人可以帮我解决它..所以我的观点是杀死刚刚启动的 python,如果另一个 python 当前正在运行..但愚蠢的是..因为我正在运行我的python并在这个python内部运行一个检查编码..它总是会退出..因为一旦我启动python PID将被创建并运行......

Okay I found a dumb thing and I want to see if someone can help me go around it.. So my point was to kill the python that is just started if another python is currently running.. but the dumb thing is.. since I am running my python and running a checking coding inside of this python.. it will always exit.. because as soon as I start the python PID will be created and will be running...

所以.. python 启动-> pid 创建并运行-> 检查python 是否正在运行-> 是python 正在运行-> 退出.

So.. python starts -> pid created and running -> check if python is running -> yes python is running -> exit.

我想看看有没有办法python启动->检查python是否已经在运行->如果它正在运行,杀死当前的python而不是正在运行的python.

I want to see if there is a way to do python starts -> check if python is already running or not -> if it is running kill the current python not the one that was running.

推荐答案

带有正在运行的进程的 PID 的锁文件是一种更系统的方法.程序只是在开始时检查 PID 文件是否存在并被锁定 (flocklockf);如果是这样,则意味着该程序的另一个实例仍在运行.如果没有,它会创建(或重写)PID 文件并锁定它.

A lockfile with PID of the running process is a more systematic way how to this. Program just checks at the start whether a PID file both exists and is locked (flock or lockf); if so, it means that another instance of this program is still running. If not, it creates (or rewrites) the PID file and locks it.

flock/lockf 锁的优点是操作系统会在程序终止时自动移除锁.

The flock/lockf lock has the advantage that operating system automatically removes the lock in case of program termination.

在此处查看如何在 Python 中执行此操作:

See here how to do this in Python:

Python:用于创建基于 PID 的锁文件的模块?

这篇关于当另一个 python 脚本正在运行时,如何停止我的 python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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