Python: module for creating PID-based lockfile?

后端 未结 7 1786
醉酒成梦
醉酒成梦 2020-12-09 03:17

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)

7条回答
  •  执笔经年
    2020-12-09 03:56

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

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

    Example usage:

    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()
    

提交回复
热议问题