Skip to content

Commit 43459f0

Browse files
committed
Added support for timeing out workers
1 parent e6655b0 commit 43459f0

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

pyres/worker.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,26 @@ def work(self, interval=5):
153153
datetime.datetime.now()))
154154

155155
try:
156-
os.waitpid(self.child, 0)
156+
start = datetime.datetime.now()
157+
result = (0, 0)
158+
timed_out = False
159+
160+
# waits for the result or times out
161+
while result == (0, 0) and not timed_out:
162+
result = os.waitpid(self.child, os.WNOHANG)
163+
now = datetime.datetime.now()
164+
165+
if self.timeout and ((now - start).seconds > self.timeout):
166+
os.kill(self.child, signal.SIGKILL)
167+
os.waitpid(-1, os.WNOHANG)
168+
timed_out = True
169+
157170
except OSError as ose:
158171
import errno
159172

160173
if ose.errno != errno.EINTR:
161174
raise ose
162-
#os.wait()
175+
163176
logger.debug('done waiting')
164177
else:
165178
self._setproctitle("Processing %s since %s" %
@@ -283,8 +296,9 @@ def worker_pids(self):
283296
grep pyres_worker").split("\n"))
284297

285298
@classmethod
286-
def run(cls, queues, server="localhost:6379", interval=None):
299+
def run(cls, queues, server="localhost:6379", interval=None, timeout=None):
287300
worker = cls(queues=queues, server=server)
301+
worker.timeout = timeout
288302
if interval is not None:
289303
worker.work(interval)
290304
else:

0 commit comments

Comments
 (0)