BVB Source Codes

rq Show timeouts.py Source code

Return Download rq: download timeouts.py Source code - Download rq Source code - Type:.py
  1. # -*- coding: utf-8 -*-
  2. from __future__ import (absolute_import, division, print_function,
  3.                         unicode_literals)
  4.  
  5. import signal
  6.  
  7.  
  8. class JobTimeoutException(Exception):
  9.     """Raised when a job takes longer to complete than the allowed maximum
  10.    timeout value.
  11.    """
  12.     pass
  13.  
  14.  
  15. class BaseDeathPenalty(object):
  16.     """Base class to setup job timeouts."""
  17.  
  18.     def __init__(self, timeout):
  19.         self._timeout = timeout
  20.  
  21.     def __enter__(self):
  22.         self.setup_death_penalty()
  23.  
  24.     def __exit__(self, type, value, traceback):
  25.         # Always cancel immediately, since we're done
  26.         try:
  27.             self.cancel_death_penalty()
  28.         except JobTimeoutException:
  29.             # Weird case: we're done with the with body, but now the alarm is
  30.             # fired.  We may safely ignore this situation and consider the
  31.             # body done.
  32.             pass
  33.  
  34.         # __exit__ may return True to supress further exception handling.  We
  35.         # don't want to suppress any exceptions here, since all errors should
  36.         # just pass through, JobTimeoutException being handled normally to the
  37.         # invoking context.
  38.         return False
  39.  
  40.     def setup_death_penalty(self):
  41.         raise NotImplementedError()
  42.  
  43.     def cancel_death_penalty(self):
  44.         raise NotImplementedError()
  45.  
  46.  
  47. class UnixSignalDeathPenalty(BaseDeathPenalty):
  48.  
  49.     def handle_death_penalty(self, signum, frame):
  50.         raise JobTimeoutException('Job exceeded maximum timeout '
  51.                                   'value ({0} seconds)'.format(self._timeout))
  52.  
  53.     def setup_death_penalty(self):
  54.         """Sets up an alarm signal and a signal handler that raises
  55.        a JobTimeoutException after the timeout amount (expressed in
  56.        seconds).
  57.        """
  58.         signal.signal(signal.SIGALRM, self.handle_death_penalty)
  59.         signal.alarm(self._timeout)
  60.  
  61.     def cancel_death_penalty(self):
  62.         """Removes the death penalty alarm and puts back the system into
  63.        default signal handling.
  64.        """
  65.         signal.alarm(0)
  66.         signal.signal(signal.SIGALRM, signal.SIG_DFL)
  67.  
downloadtimeouts.py Source code - Download rq Source code
Related Source Codes/Software:
amazon-dsstne - Deep Scalable Sparse Tensor Network Engine (DSSTNE... 2017-01-08
webpack-demos - a collection of simple demos of Webpack 2017-01-08
Squire - HTML5 rich text editor. Try the demo integration a... 2017-01-08
thor - Thor is a toolkit for building powerful command-li... 2017-01-08
glide - Package Management for Golang h... 2017-01-08
emmet-vim - emmet for vim: http://emmet.io/ ... 2017-01-08
prose - A Content Editor for GitHub. ht... 2017-01-08
sshrc - ring your .bashrc, .vimrc, etc. with you when you ... 2017-01-08
typed.js - A jQuery typing animation script. ... 2017-01-08
find - High-precision indoor positioning framework for mo... 2017-01-08
discover-flask - Full Stack Web Development with Flask. ... 2017-01-12
spring-mvc-showcase - Demonstrates the features of the Spring MVC web fr... 2017-01-12
tushare - TuShare is a utility for crawling historical data ... 2017-01-12
raml-spec - RAML Specification http://raml.... 2017-01-12
android-stackblur - Android StackBlur is a library that can perform a ... 2017-01-12
sound-redux - A Soundcloud client built with React / Redux ... 2017-01-12
httpstat - curl statistics made simple 2017-01-12
vim - Vim configuration file and plug-ins 2017-01-12
appframework - The definitive HTML5 mobile javascript framework ... 2017-01-12
BaiduExporter - Assistant for Baidu to export download links to ar... 2017-01-11

 Back to top