base.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.concurrency')
  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 supports rate limits.
  29. #: (this is here for gevent, which currently does not implement
  30. #: the necessary timers).
  31. rlimit_safe = True
  32. #: set to true if pool requires the use of a mediator
  33. #: thread (e.g. if applying new items can block the current thread).
  34. requires_mediator = False
  35. #: set to true if pool uses greenlets.
  36. is_green = False
  37. _state = None
  38. _pool = None
  39. #: only used by multiprocessing pool
  40. uses_semaphore = False
  41. def __init__(self, limit=None, putlocks=True, forking_enable=True,
  42. **options):
  43. self.limit = limit
  44. self.putlocks = putlocks
  45. self.options = options
  46. self.forking_enable = forking_enable
  47. self._does_debug = logger.isEnabledFor(logging.DEBUG)
  48. def on_start(self):
  49. pass
  50. def did_start_ok(self):
  51. return True
  52. def on_stop(self):
  53. pass
  54. def on_apply(self, *args, **kwargs):
  55. pass
  56. def on_terminate(self):
  57. pass
  58. def on_soft_timeout(self, job):
  59. pass
  60. def on_hard_timeout(self, job):
  61. pass
  62. def maybe_handle_result(self, *args):
  63. pass
  64. def maintain_pool(self, *args, **kwargs):
  65. pass
  66. def terminate_job(self, pid):
  67. raise NotImplementedError(
  68. '{0} does not implement kill_job'.format(type(self)))
  69. def restart(self):
  70. raise NotImplementedError(
  71. '{0} does not implement restart'.format(type(self)))
  72. def stop(self):
  73. self.on_stop()
  74. self._state = self.TERMINATE
  75. def terminate(self):
  76. self._state = self.TERMINATE
  77. self.on_terminate()
  78. def start(self):
  79. self.on_start()
  80. self._state = self.RUN
  81. def close(self):
  82. self._state = self.CLOSE
  83. self.on_close()
  84. def on_close(self):
  85. pass
  86. def init_callbacks(self, **kwargs):
  87. pass
  88. def apply_async(self, target, args=[], kwargs={}, **options):
  89. """Equivalent of the :func:`apply` built-in function.
  90. Callbacks should optimally return as soon as possible since
  91. otherwise the thread which handles the result will get blocked.
  92. """
  93. if self._does_debug:
  94. logger.debug('TaskPool: Apply %s (args:%s kwargs:%s)',
  95. target, safe_repr(args), safe_repr(kwargs))
  96. return self.on_apply(target, args, kwargs,
  97. waitforslot=self.putlocks,
  98. **options)
  99. def _get_info(self):
  100. return {}
  101. @property
  102. def info(self):
  103. return self._get_info()
  104. @property
  105. def active(self):
  106. return self._state == self.RUN
  107. @property
  108. def num_processes(self):
  109. return self.limit
  110. @property
  111. def readers(self):
  112. return {}
  113. @property
  114. def writers(self):
  115. return {}
  116. @property
  117. def timers(self):
  118. return {}