base.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.concurrency.base
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. TaskPool interface.
  6. """
  7. from __future__ import absolute_import
  8. import logging
  9. import os
  10. import time
  11. from kombu.utils.encoding import safe_repr
  12. from celery.utils import timer2
  13. from celery.utils.log import get_logger
  14. logger = get_logger('celery.pool')
  15. def apply_target(target, args=(), kwargs={}, callback=None,
  16. accept_callback=None, pid=None, **_):
  17. if accept_callback:
  18. accept_callback(pid or os.getpid(), time.time())
  19. callback(target(*args, **kwargs))
  20. class BasePool(object):
  21. RUN = 0x1
  22. CLOSE = 0x2
  23. TERMINATE = 0x3
  24. Timer = timer2.Timer
  25. #: set to true if the pool can be shutdown from within
  26. #: a signal handler.
  27. signal_safe = True
  28. #: set to true if pool uses greenlets.
  29. is_green = False
  30. _state = None
  31. _pool = None
  32. #: only used by multiprocessing pool
  33. uses_semaphore = False
  34. def __init__(self, limit=None, putlocks=True,
  35. forking_enable=True, callbacks_propagate=(), **options):
  36. self.limit = limit
  37. self.putlocks = putlocks
  38. self.options = options
  39. self.forking_enable = forking_enable
  40. self.callbacks_propagate = callbacks_propagate
  41. self._does_debug = logger.isEnabledFor(logging.DEBUG)
  42. def on_start(self):
  43. pass
  44. def did_start_ok(self):
  45. return True
  46. def flush(self):
  47. pass
  48. def on_stop(self):
  49. pass
  50. def on_apply(self, *args, **kwargs):
  51. pass
  52. def on_terminate(self):
  53. pass
  54. def on_soft_timeout(self, job):
  55. pass
  56. def on_hard_timeout(self, job):
  57. pass
  58. def maybe_handle_result(self, *args):
  59. pass
  60. def maintain_pool(self, *args, **kwargs):
  61. pass
  62. def terminate_job(self, pid):
  63. raise NotImplementedError(
  64. '{0} does not implement kill_job'.format(type(self)))
  65. def restart(self):
  66. raise NotImplementedError(
  67. '{0} does not implement restart'.format(type(self)))
  68. def stop(self):
  69. self.on_stop()
  70. self._state = self.TERMINATE
  71. def terminate(self):
  72. self._state = self.TERMINATE
  73. self.on_terminate()
  74. def start(self):
  75. self.on_start()
  76. self._state = self.RUN
  77. def close(self):
  78. self._state = self.CLOSE
  79. self.on_close()
  80. def on_close(self):
  81. pass
  82. def init_callbacks(self, **kwargs):
  83. pass
  84. def apply_async(self, target, args=[], kwargs={}, **options):
  85. """Equivalent of the :func:`apply` built-in function.
  86. Callbacks should optimally return as soon as possible since
  87. otherwise the thread which handles the result will get blocked.
  88. """
  89. if self._does_debug:
  90. logger.debug('TaskPool: Apply %s (args:%s kwargs:%s)',
  91. target, safe_repr(args), safe_repr(kwargs))
  92. return self.on_apply(target, args, kwargs,
  93. waitforslot=self.putlocks,
  94. callbacks_propagate=self.callbacks_propagate,
  95. **options)
  96. def _get_info(self):
  97. return {}
  98. @property
  99. def info(self):
  100. return self._get_info()
  101. @property
  102. def active(self):
  103. return self._state == self.RUN
  104. @property
  105. def num_processes(self):
  106. return self.limit
  107. @property
  108. def readers(self):
  109. return {}
  110. @property
  111. def writers(self):
  112. return {}
  113. @property
  114. def timers(self):
  115. return {}