3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from Queue import Queue
16 from cmframework.server import cmeventletrwlock
17 from cmframework.server import cmactivatorworker
20 class CMActivator(object):
21 def __init__(self, worker_count):
23 self.node_works = Queue()
26 self.worker_count = worker_count
27 self.lock = cmeventletrwlock.CMEventletRWLock()
29 def add_handler(self, handler):
30 self.handlers.append(handler)
32 def get_parallel_work(self):
33 return self.node_works.get()
36 return self.works.get()
38 def add_work(self, work):
40 if not work.get_target():
43 self.node_works.put(work)
45 def get_handlers(self):
49 worker = cmactivatorworker.CMActivatorWorker(self, 0, self.lock)
51 self.workers.append(worker)
53 for i in range(1, self.worker_count+1):
54 worker = cmactivatorworker.CMActivatorWorker(self, i, self.lock, parallel=True)
56 self.workers.append(worker)