/circuits/core/pools.py

https://bitbucket.org/prologic/circuits/ · Python · 49 lines · 30 code · 14 blank · 5 comment · 5 complexity · 20b36fb03d3f128f78b46ba3b31d103c MD5 · raw file

  1. # Module: pools
  2. # Date: 6th February 2011
  3. # Author: James Mills, prologic at shortcircuit dot net dot au
  4. """Pools
  5. Thread and Process based "worker" pools.
  6. """
  7. from time import time
  8. from uuid import uuid4 as uuid
  9. from random import seed, choice
  10. from circuits.core.workers import Task, Worker
  11. from circuits.core import handler, BaseComponent
  12. seed(time())
  13. class Pool(BaseComponent):
  14. channel = "pool"
  15. def __init__(self, min=5, max=10, process=False, channel=channel):
  16. super(Pool, self).__init__(channel=channel)
  17. self._workers = []
  18. for i in range(min):
  19. self._workers.append(Worker(process=process, channel=str(uuid())))
  20. @handler("task")
  21. def _on_task(self, f, *args, **kwargs):
  22. workers = float(len(self._workers))
  23. tasks = [float(len(worker)) for worker in self._workers]
  24. total = sum(tasks)
  25. _avg = total / workers
  26. assigned = None
  27. for worker in self._workers:
  28. if len(worker) < _avg:
  29. assigned = worker.channel
  30. return worker.push(Task(f, *args, **kwargs), target=worker)
  31. if not assigned:
  32. worker = choice(self._workers)
  33. assigned = worker.channel
  34. return worker.fire(Task(f, *args, **kwargs), target=worker)