本文整理汇总了Python中pyres.ResQ.size方法的典型用法代码示例。如果您正苦于以下问题:Python ResQ.size方法的具体用法?Python ResQ.size怎么用?Python ResQ.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyres.ResQ
的用法示例。
在下文中一共展示了ResQ.size方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Manager
# 需要导入模块: from pyres import ResQ [as 别名]
# 或者: from pyres.ResQ import size [as 别名]
class Manager(object):
def __init__(self, queues, host, max_children=10):
self.queues = queues
self._host = host
self.max_children = max_children
self._shutdown = False
self.children = []
self.resq = ResQ(host)
self.validate_queues()
self.reports = {}
def __str__(self):
import os;
hostname = os.uname()[1]
pid = os.getpid()
return 'Manager:%s:%s:%s' % (hostname, pid, ','.join(self.queues))
def validate_queues(self):
if not self.queues:
raise NoQueueError("Please give each worker at least one queue.")
def check_rising(self, queue, size):
if queue in self.reports:
time = time.time()
old_size = self.reports[queue][0]
old_time = self.reports[queue][1]
if time > old_time + 5 and size > old_size + 20:
return True
else:
self.reports[queue] = (size, time.time())
return False
def work(self):
self.startup()
while True:
if self._shutdown:
break
#check to see if stuff is still going
for queue in self.queues:
#check queue size
size = self.resq.size(queue)
if check_rising(queue,size):
if len(self.children) < self.max_children:
self.start_child(queue)
def startup(self):
self.register_manager()
self.register_signals()
def register_manager(self):
self.resq.redis.sadd('managers',str(self))
def unregister_manager(self):
self.resq.redis.srem('managers',str(self))
def register_signals(self):
signal.signal(signal.SIGTERM, self.shutdown_all)
signal.signal(signal.SIGINT, self.shutdown_all)
signal.signal(signal.SIGQUIT, self.schedule_shutdown)
signal.signal(signal.SIGUSR1, self.kill_children)
def shutdown_all(self, signum, frame):
self.schedule_shutdown(signum, frame)
self.kill_children(signum, frame)
def schedule_shutdown(self, signum, frame):
self._shutdown = True
def kill_children(self):
for child in self.children:
child.terminate()
def start_child(self, queue):
p = multiprocessing.Process(target=JuniorWorker.run, args=([queue], self._host))
self.children.append(p)
p.start()
return True
@classmethod
def run(cls, queues=[], host="localhost:6379"):
manager = cls(queues, host)
manager.work()