Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

threads.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.concurrency.threads
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Pool implementation using threads.
  6. """
  7. from __future__ import absolute_import
  8. from celery.five import UserDict
  9. from .base import apply_target, BasePool
  10. __all__ = ['TaskPool']
  11. class NullDict(UserDict):
  12. def __setitem__(self, key, value):
  13. pass
  14. class TaskPool(BasePool):
  15. def __init__(self, *args, **kwargs):
  16. try:
  17. import threadpool
  18. except ImportError:
  19. raise ImportError(
  20. 'The threaded pool requires the threadpool module.')
  21. self.WorkRequest = threadpool.WorkRequest
  22. self.ThreadPool = threadpool.ThreadPool
  23. super(TaskPool, self).__init__(*args, **kwargs)
  24. def on_start(self):
  25. self._pool = self.ThreadPool(self.limit)
  26. # threadpool stores all work requests until they are processed
  27. # we don't need this dict, and it occupies way too much memory.
  28. self._pool.workRequests = NullDict()
  29. self._quick_put = self._pool.putRequest
  30. self._quick_clear = self._pool._results_queue.queue.clear
  31. def on_stop(self):
  32. self._pool.dismissWorkers(self.limit, do_join=True)
  33. def on_apply(self, target, args=None, kwargs=None, callback=None,
  34. accept_callback=None, **_):
  35. req = self.WorkRequest(apply_target, (target, args, kwargs, callback,
  36. accept_callback))
  37. self._quick_put(req)
  38. # threadpool also has callback support,
  39. # but for some reason the callback is not triggered
  40. # before you've collected the results.
  41. # Clear the results (if any), so it doesn't grow too large.
  42. self._quick_clear()
  43. return req