pidfile.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # daemon/pidfile.py
  2. # Part of ‘python-daemon’, an implementation of PEP 3143.
  3. #
  4. # This is free software, and you are welcome to redistribute it under
  5. # certain conditions; see the end of this file for copyright
  6. # information, grant of license, and disclaimer of warranty.
  7. """ Lockfile behaviour implemented via Unix PID files. """
  8. from lockfile.pidlockfile import PIDLockFile
  9. class TimeoutPIDLockFile(PIDLockFile):
  10. """ Lockfile with default timeout, implemented as a Unix PID file.
  11. This uses the ``PIDLockFile`` implementation, with the
  12. following changes:
  13. * The `acquire_timeout` parameter to the initialiser will be
  14. used as the default `timeout` parameter for the `acquire`
  15. method.
  16. """
  17. def __init__(self, path, acquire_timeout=None, *args, **kwargs):
  18. """ Set up the parameters of a TimeoutPIDLockFile.
  19. :param path: Filesystem path to the PID file.
  20. :param acquire_timeout: Value to use by default for the
  21. `acquire` call.
  22. :return: ``None``.
  23. """
  24. self.acquire_timeout = acquire_timeout
  25. super().__init__(path, *args, **kwargs)
  26. def acquire(self, timeout=None, *args, **kwargs):
  27. """ Acquire the lock.
  28. :param timeout: Specifies the timeout; see below for valid
  29. values.
  30. :return: ``None``.
  31. The `timeout` defaults to the value set during
  32. initialisation with the `acquire_timeout` parameter. It is
  33. passed to `PIDLockFile.acquire`; see that method for
  34. details.
  35. """
  36. if timeout is None:
  37. timeout = self.acquire_timeout
  38. super().acquire(timeout, *args, **kwargs)
  39. # Copyright © 2008–2024 Ben Finney <ben+python@benfinney.id.au>
  40. #
  41. # This is free software: you may copy, modify, and/or distribute this work
  42. # under the terms of the Apache License, version 2.0 as published by the
  43. # Apache Software Foundation.
  44. # No warranty expressed or implied. See the file ‘LICENSE.ASF-2’ for details.
  45. # Local variables:
  46. # coding: utf-8
  47. # mode: python
  48. # End:
  49. # vim: fileencoding=utf-8 filetype=python :