MCPcopy Create free account
hub / github.com/celery/celery / Pidfile

Class Pidfile

celery/platforms.py:134–247  ·  view source on GitHub ↗

Pidfile. This is the type returned by :func:`create_pidlock`. See Also: Best practice is to not use this directly but rather use the :func:`create_pidlock` function instead: more convenient and also removes stale pidfiles (when the process holding the lock i

Source from the content-addressed store, hash-verified

132
133
134class Pidfile:
135 """Pidfile.
136
137 This is the type returned by :func:`create_pidlock`.
138
139 See Also:
140 Best practice is to not use this directly but rather use
141 the :func:`create_pidlock` function instead:
142 more convenient and also removes stale pidfiles (when
143 the process holding the lock is no longer running).
144 """
145
146 #: Path to the pid lock file.
147 path = None
148
149 def __init__(self, path):
150 self.path = os.path.abspath(path)
151
152 def acquire(self):
153 """Acquire lock."""
154 try:
155 self.write_pid()
156 except OSError as exc:
157 reraise(LockFailed, LockFailed(str(exc)), sys.exc_info()[2])
158 return self
159
160 __enter__ = acquire
161
162 def is_locked(self):
163 """Return true if the pid lock exists."""
164 return os.path.exists(self.path)
165
166 def release(self, *args):
167 """Release lock."""
168 self.remove()
169
170 __exit__ = release
171
172 def read_pid(self):
173 """Read and return the current pid."""
174 with ignore_errno('ENOENT'):
175 with open(self.path) as fh:
176 line = fh.readline()
177 if line.strip() == line: # must contain '\n'
178 raise ValueError(
179 f'Partial or invalid pidfile {self.path}')
180
181 try:
182 return int(line.strip())
183 except ValueError:
184 raise ValueError(
185 f'pidfile {self.path} contents invalid.')
186
187 def remove(self):
188 """Remove the lock."""
189 with ignore_errno(errno.ENOENT, errno.EACCES):
190 os.unlink(self.path)
191

Callers 15

test_contextMethod · 0.90
test_is_lockedMethod · 0.90
test_read_pidMethod · 0.90
test_removeMethod · 0.90
test_remove_ENOENTMethod · 0.90
test_remove_EACCESMethod · 0.90
test_remove_OSErrorMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_contextMethod · 0.72
test_is_lockedMethod · 0.72
test_read_pidMethod · 0.72
test_removeMethod · 0.72
test_remove_ENOENTMethod · 0.72
test_remove_EACCESMethod · 0.72
test_remove_OSErrorMethod · 0.72