__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import os
  4. import platform
  5. import signal as _signal
  6. from celery import platforms
  7. from celery import signals
  8. from celery.app import app_or_default
  9. from celery.concurrency.base import BasePool
  10. from billiard.pool import Pool, RUN
  11. if platform.system() == "Windows": # pragma: no cover
  12. # On Windows os.kill calls TerminateProcess which cannot be
  13. # handled by # any process, so this is needed to terminate the task
  14. # *and its children* (if any).
  15. from ._win import kill_processtree as _kill # noqa
  16. else:
  17. from os import kill as _kill # noqa
  18. #: List of signals to reset when a child process starts.
  19. WORKER_SIGRESET = frozenset(["SIGTERM",
  20. "SIGHUP",
  21. "SIGTTIN",
  22. "SIGTTOU",
  23. "SIGUSR1"])
  24. #: List of signals to ignore when a child process starts.
  25. WORKER_SIGIGNORE = frozenset(["SIGINT"])
  26. def process_initializer(app, hostname):
  27. """Initializes the process so it can be used to process tasks."""
  28. app = app_or_default(app)
  29. app.set_current()
  30. platforms.signals.reset(*WORKER_SIGRESET)
  31. platforms.signals.ignore(*WORKER_SIGIGNORE)
  32. platforms.set_mp_process_title("celeryd", hostname=hostname)
  33. # This is for Windows and other platforms not supporting
  34. # fork(). Note that init_worker makes sure it's only
  35. # run once per process.
  36. app.log.setup(int(os.environ.get("CELERY_LOG_LEVEL", 0)),
  37. os.environ.get("CELERY_LOG_FILE") or None,
  38. bool(os.environ.get("CELERY_LOG_REDIRECT", False)),
  39. str(os.environ.get("CELERY_LOG_REDIRECT_LEVEL")))
  40. app.loader.init_worker()
  41. app.loader.init_worker_process()
  42. signals.worker_process_init.send(sender=None)
  43. class TaskPool(BasePool):
  44. """Multiprocessing Pool implementation."""
  45. Pool = Pool
  46. requires_mediator = True
  47. def on_start(self):
  48. """Run the task pool.
  49. Will pre-fork all workers so they're ready to accept tasks.
  50. """
  51. self._pool = self.Pool(processes=self.limit,
  52. initializer=process_initializer,
  53. **self.options)
  54. self.on_apply = self._pool.apply_async
  55. def on_stop(self):
  56. """Gracefully stop the pool."""
  57. if self._pool is not None and self._pool._state == RUN:
  58. self._pool.close()
  59. self._pool.join()
  60. self._pool = None
  61. def on_terminate(self):
  62. """Force terminate the pool."""
  63. if self._pool is not None:
  64. self._pool.terminate()
  65. self._pool = None
  66. def terminate_job(self, pid, signal=None):
  67. _kill(pid, signal or _signal.SIGTERM)
  68. def grow(self, n=1):
  69. return self._pool.grow(n)
  70. def shrink(self, n=1):
  71. return self._pool.shrink(n)
  72. def restart(self):
  73. self._pool.restart()
  74. def _get_info(self):
  75. return {"max-concurrency": self.limit,
  76. "processes": [p.pid for p in self._pool._pool],
  77. "max-tasks-per-child": self._pool._maxtasksperchild,
  78. "put-guarded-by-semaphore": self.putlocks,
  79. "timeouts": (self._pool.soft_timeout, self._pool.timeout)}
  80. @property
  81. def num_processes(self):
  82. return self._pool._processes