且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当另一个 python 脚本正在运行时,如何停止我的 python 脚本?

更新时间:2022-05-24 22:35:40

带有正在运行的进程的 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 的锁文件的模块?