本文整理汇总了Python中celery.datastructures.LocalCache.values方法的典型用法代码示例。如果您正苦于以下问题:Python LocalCache.values方法的具体用法?Python LocalCache.values怎么用?Python LocalCache.values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类celery.datastructures.LocalCache
的用法示例。
在下文中一共展示了LocalCache.values方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: State
# 需要导入模块: from celery.datastructures import LocalCache [as 别名]
# 或者: from celery.datastructures.LocalCache import values [as 别名]
#.........这里部分代码省略.........
try:
worker = self.workers[hostname]
worker.update(kwargs)
except KeyError:
worker = self.workers[hostname] = Worker(
hostname=hostname, **kwargs)
return worker
def get_or_create_task(self, uuid):
"""Get or create task by uuid."""
try:
return self.tasks[uuid]
except KeyError:
task = self.tasks[uuid] = Task(uuid=uuid)
return task
def worker_event(self, type, fields):
"""Process worker event."""
hostname = fields.pop("hostname", None)
if hostname:
worker = self.get_or_create_worker(hostname)
handler = getattr(worker, "on_%s" % type, None)
if handler:
handler(**fields)
def task_event(self, type, fields):
"""Process task event."""
uuid = fields.pop("uuid")
hostname = fields.pop("hostname")
worker = self.get_or_create_worker(hostname)
task = self.get_or_create_task(uuid)
handler = getattr(task, "on_%s" % type, None)
if type == "received":
self.task_count += 1
if handler:
handler(**fields)
task.worker = worker
def _dispatch_event(self, event):
self.event_count += 1
event = kwdict(event)
group, _, type = partition(event.pop("type"), "-")
self.group_handlers[group](type, event)
if self.event_callback:
self.event_callback(self, event)
def event(self, event):
"""Process event."""
try:
if not self.frozen:
self._dispatch_event(event)
elif self._buffering:
self.buffer.append(event)
finally:
pass
def tasks_by_timestamp(self, limit=None):
"""Get tasks by timestamp.
Returns a list of ``(uuid, task)`` tuples.
"""
return self._sort_tasks_by_time(self.tasks.items()[:limit])
def _sort_tasks_by_time(self, tasks):
"""Sort task items by time."""
return sorted(tasks, key=lambda t: t[1].timestamp,
reverse=True)
def tasks_by_type(self, name, limit=None):
"""Get all tasks by type.
Returns a list of ``(uuid, task)`` tuples.
"""
return self._sort_tasks_by_time([(uuid, task)
for uuid, task in self.tasks.items()[:limit]
if task.name == name])
def tasks_by_worker(self, hostname, limit=None):
"""Get all tasks by worker.
Returns a list of ``(uuid, task)`` tuples.
"""
return self._sort_tasks_by_time([(uuid, task)
for uuid, task in self.tasks.items()[:limit]
if task.worker.hostname == hostname])
def task_types(self):
"""Returns a list of all seen task types."""
return list(sorted(set(task.name for task in self.tasks.values())))
def alive_workers(self):
"""Returns a list of (seemingly) alive workers."""
return [w for w in self.workers.values() if w.alive]
def __repr__(self):
return "<ClusterState: events=%s tasks=%s>" % (self.event_count,
self.task_count)
示例2: State
# 需要导入模块: from celery.datastructures import LocalCache [as 别名]
# 或者: from celery.datastructures.LocalCache import values [as 别名]
#.........这里部分代码省略.........
def get_or_create_worker(self, hostname, **kwargs):
"""Get or create worker by hostname."""
try:
worker = self.workers[hostname]
worker.update(kwargs)
except KeyError:
worker = self.workers[hostname] = Worker(
hostname=hostname, **kwargs)
return worker
def get_or_create_task(self, uuid):
"""Get or create task by uuid."""
try:
return self.tasks[uuid]
except KeyError:
task = self.tasks[uuid] = Task(uuid=uuid)
return task
def worker_event(self, type, fields):
"""Process worker event."""
hostname = fields.pop("hostname", None)
if hostname:
worker = self.get_or_create_worker(hostname)
handler = getattr(worker, "on_%s" % type, None)
if handler:
handler(**fields)
def task_event(self, type, fields):
"""Process task event."""
uuid = fields.pop("uuid")
hostname = fields.pop("hostname")
worker = self.get_or_create_worker(hostname)
task = self.get_or_create_task(uuid)
handler = getattr(task, "on_%s" % type, None)
if type == "received":
self.task_count += 1
if handler:
handler(**fields)
task.worker = worker
def event(self, event):
self._mutex.acquire()
try:
return self._dispatch_event(event)
finally:
self._mutex.release()
def _dispatch_event(self, event):
self.event_count += 1
event = kwdict(event)
group, _, type = partition(event.pop("type"), "-")
self.group_handlers[group](type, event)
if self.event_callback:
self.event_callback(self, event)
def tasks_by_timestamp(self, limit=None):
"""Get tasks by timestamp.
Returns a list of ``(uuid, task)`` tuples.
"""
return self._sort_tasks_by_time(self.tasks.items()[:limit])
def _sort_tasks_by_time(self, tasks):
"""Sort task items by time."""
return sorted(tasks, key=lambda t: t[1].timestamp,
reverse=True)
def tasks_by_type(self, name, limit=None):
"""Get all tasks by type.
Returns a list of ``(uuid, task)`` tuples.
"""
return self._sort_tasks_by_time([(uuid, task)
for uuid, task in self.tasks.items()[:limit]
if task.name == name])
def tasks_by_worker(self, hostname, limit=None):
"""Get all tasks by worker.
Returns a list of ``(uuid, task)`` tuples.
"""
return self._sort_tasks_by_time([(uuid, task)
for uuid, task in self.tasks.items()[:limit]
if task.worker.hostname == hostname])
def task_types(self):
"""Returns a list of all seen task types."""
return list(sorted(set(task.name for task in self.tasks.values())))
def alive_workers(self):
"""Returns a list of (seemingly) alive workers."""
return [w for w in self.workers.values() if w.alive]
def __repr__(self):
return "<ClusterState: events=%s tasks=%s>" % (self.event_count,
self.task_count)
示例3: State
# 需要导入模块: from celery.datastructures import LocalCache [as 别名]
# 或者: from celery.datastructures.LocalCache import values [as 别名]
class State(object):
"""Represents a snapshot of a clusters state."""
event_count = 0
task_count = 0
def __init__(self, callback=None,
max_workers_in_memory=5000, max_tasks_in_memory=10000):
self.workers = LocalCache(max_workers_in_memory)
self.tasks = LocalCache(max_tasks_in_memory)
self.event_callback = callback
self.group_handlers = {"worker": self.worker_event,
"task": self.task_event}
def get_or_create_worker(self, hostname, **kwargs):
"""Get or create worker by hostname."""
try:
worker = self.workers[hostname]
worker.update(kwargs)
except KeyError:
worker = self.workers[hostname] = Worker(
hostname=hostname, **kwargs)
return worker
def get_or_create_task(self, uuid, **kwargs):
"""Get or create task by uuid."""
try:
task = self.tasks[uuid]
task.update(kwargs)
except KeyError:
task = self.tasks[uuid] = Task(uuid=uuid, **kwargs)
return task
def worker_event(self, type, fields):
"""Process worker event."""
hostname = fields.pop("hostname")
worker = self.get_or_create_worker(hostname)
handler = getattr(worker, "on_%s" % type)
if handler:
handler(**fields)
def task_event(self, type, fields):
"""Process task event."""
uuid = fields.pop("uuid")
hostname = fields.pop("hostname")
worker = self.get_or_create_worker(hostname)
task = self.get_or_create_task(uuid)
handler = getattr(task, "on_%s" % type)
if type == "received":
self.task_count += 1
if handler:
handler(**fields)
task.worker = worker
def event(self, event):
"""Process event."""
self.event_count += 1
event = kwdict(event)
group, _, type = partition(event.pop("type"), "-")
self.group_handlers[group](type, event)
if self.event_callback:
self.event_callback(self, event)
def tasks_by_timestamp(self):
"""Get tasks by timestamp.
Returns a list of ``(uuid, task)`` tuples.
"""
return self._sort_tasks_by_time(self.tasks.items())
def _sort_tasks_by_time(self, tasks):
"""Sort task items by time."""
return sorted(tasks, key=lambda t: t[1].timestamp, reverse=True)
def tasks_by_type(self, name):
"""Get all tasks by type.
Returns a list of ``(uuid, task)`` tuples.
"""
return self._sort_tasks_by_time([(uuid, task)
for uuid, task in self.tasks.items()
if task.name == name])
def tasks_by_worker(self, hostname):
"""Get all tasks by worker.
Returns a list of ``(uuid, task)`` tuples.
"""
return self._sort_tasks_by_time([(uuid, task)
for uuid, task in self.tasks.items()
if task.worker.hostname == hostname])
def task_types(self):
"""Returns a list of all seen task types."""
return list(set(task.name for task in self.tasks.values()))
def alive_workers(self):
"""Returns a list of (seemingly) alive workers."""
#.........这里部分代码省略.........