PageRenderTime 27ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/circuits/core/pools.py

https://bitbucket.org/prologic/circuits/
Python | 49 lines | 43 code | 1 blank | 5 comment | 0 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
 5"""Pools
 6
 7Thread and Process based "worker" pools.
 8"""
 9
10from time import time
11from uuid import uuid4 as uuid
12from random import seed, choice
13
14from circuits.core.workers import Task, Worker
15from circuits.core import handler, BaseComponent
16
17seed(time())
18
19
20class Pool(BaseComponent):
21
22    channel = "pool"
23
24    def __init__(self, min=5, max=10, process=False, channel=channel):
25        super(Pool, self).__init__(channel=channel)
26
27        self._workers = []
28
29        for i in range(min):
30            self._workers.append(Worker(process=process, channel=str(uuid())))
31
32    @handler("task")
33    def _on_task(self, f, *args, **kwargs):
34        workers = float(len(self._workers))
35        tasks = [float(len(worker)) for worker in self._workers]
36        total = sum(tasks)
37        _avg = total / workers
38
39        assigned = None
40
41        for worker in self._workers:
42            if len(worker) < _avg:
43                assigned = worker.channel
44                return worker.push(Task(f, *args, **kwargs), target=worker)
45
46        if not assigned:
47            worker = choice(self._workers)
48            assigned = worker.channel
49            return worker.fire(Task(f, *args, **kwargs), target=worker)