Python:用于创建基于PID的锁定文件的模块? [英] Python: module for creating PID-based lockfile?

查看:118
本文介绍了Python:用于创建基于PID的锁定文件的模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个可能运行很长时间(取决于一堆东西)的Python脚本,并且我想确保多个实例(通过cron启动)不会踩踏彼此的脚趾.这样做的逻辑方法似乎是基于PID的锁定文件……但是,如果已有代码可以执行此操作,我不想重新发明轮子.

I'm writing a Python script that may or may not (depending on a bunch of things) run for a long time, and I'd like to make sure that multiple instances (started via cron) don't step on each others toes. The logical way to do this seems to be a PID-based lockfile… But I don't want to re-invent the wheel if there is already code to do this.

那么,是否有一个Python模块可以管理基于PID的锁定文件的详细信息?

So, is there a Python module out there which will manage the details of a PID-based lockfile?

推荐答案

如果您可以使用GPLv2,那么Mercurial可以使用该模块:

If you can use GPLv2, Mercurial has a module for that:

http://bitbucket.org/mirror/mercurial/src/tip/mercurial/lock.py

示例用法:

from mercurial import error, lock

try:
    l = lock.lock("/path/to/lock", timeout=600) # wait at most 10 minutes
    # do something
except error.LockHeld:
     # couldn't take the lock
else:
    l.release()

这篇关于Python:用于创建基于PID的锁定文件的模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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