Python: module for creating PID-based lockfile?

后端 未结 7 1798
醉酒成梦
醉酒成梦 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 04:05

    You can try PID: https://pypi.org/project/pid/

    As the documentation shows, you can lock a function simply adding the decorator @pidfile() on the top of function/method name.

    from pid.decorator import pidfile
    
    
    @pidfile()
    def main():
      pass
    
    if __name__ == "__main__":
      main()
    

    The default location for pidfile self check (the file who says if you can execute the code or not) is '/var/run'. You can change it as follows:

    @pidfile(piddir='/path/to/a/custom/location')
    

    For other params, see: https://github.com/trbs/pid/blob/95499b30e8ec4a473c0e6b407c03ce644f61c643/pid/base.py#L41

    Unfortunatly, this lib's documentation is a little bit poor.

提交回复
热议问题