python代码分析-Python-Diamond的代码分析

...

# 路劲加载

sys.path.append(

os.path.abspath(

os.path.join(

os.path.dirname(__file__), "../")))

class Server(object):

"""

Server class loads and starts Handlers and Collectors

"""

def __init__(self, configfile):

# Initialize Logging

self.log = logging.getLogger('diamond')

# Initialize Members

self.configfile = configfile

self.config = None

self.handlers = []

self.handler_queue = []

self.modules = {}

self.metric_queue = None #对应的队列

def run(self):

"""

Load handler and collector classes and then start collectors

"""

#######################################################################

# Config

#######################################################################

self.config = load_config(self.configfile) #加载对应的配置文件

collectors = load_collectors(self.config['server']['collectors_path']) #获取对应的收集对象

metric_queue_size = int(self.config['server'].get('metric_queue_size',

16384))

self.metric_queue = self.manager.Queue(maxsize=metric_queue_size) #创建进程可用的队列

self.log.debug('metric_queue_size: %d', metric_queue_size)

if 'handlers_path' in self.config['server']:

handlers_path = self.config['server']['handlers_path']

# Make an list if not one

if isinstance(handlers_path, basestring):

handlers_path = handlers_path.split(',')

handlers_path = map(str.strip, handlers_path)

self.config['server']['handlers_path'] = handlers_path

load_include_path(handlers_path)

if 'handlers' not in self.config['server']:

self.log.critical('handlers missing from server section in config')

sys.exit(1)

handlers = self.config['server'].get('handlers')

if isinstance(handlers, basestring):

handlers = [handlers]

# Prevent the Queue Handler from being a normal handler

if 'diamond.handler.queue.QueueHandler' in handlers:

handlers.remove('diamond.handler.queue.QueueHandler')

self.handlers = load_handlers(self.config, handlers) #加载对应的处理器

QueueHandler = load_dynamic_class(

'diamond.handler.queue.QueueHandler',

Handler

) #获取一个处理器队列

self.handler_queue = QueueHandler(

config=self.config, queue=self.metric_queue, log=self.log)

process = multiprocessing.Process( #只产生一个进程

name="Handlers",

target=handler_process, # 参数为handler_process,调用handler处理对应的发送数据

args=(self.handlers, self.metric_queue, self.log), #对应的参数如下

)

process.daemon = True #设置为daemon函数

process.start() #启动对应的进程

if hasattr(signal, 'SIGHUP'):

signal.signal(signal.SIGHUP, signal_to_exception) #安装对应的异常处理函数

#######################################################################

while True:

try:

active_children = multiprocessing.active_children() #获取所有正常运行的子进程

running_processes = []

for process in active_children:

running_processes.append(process.name) #保存子进程的名字

running_processes = set(running_processes)

##############################################################

# Collectors

##############################################################

running_collectors = [] #获取默认启动的Collecter

for collector, config in self.config['collectors'].iteritems():

if config.get('enabled', False) is not True:

continue

running_collectors.append(collector) #添加启动的Collecter

running_collectors = set(running_collectors)

# Collectors that are running but shouldn't be

for process_name in running_processes - running_collectors: #关闭不再处理的Collecter进程

if 'Collector' not in process_name:

continue

for process in active_children:

if process.name == process_name:

process.terminate() #终止对应的进程

collector_classes = dict(

(cls.__name__.split('.')[-1], cls) #键值和对应的cls

for cls in collectors.values()

)

load_delay = self.config['server'].get('collectors_load_delay',

1.0)

for process_name in running_collectors - running_processes: #当前还没有运行的子进程

# To handle running multiple collectors concurrently, we

# split on white space and use the first word as the

# collector name to spin

collector_name = process_name.split()[0]

if 'Collector' not in collector_name:

continue

if collector_name not in collector_classes:

self.log.error('Can not find collector %s',

collector_name)

continue

collector = initialize_collector(

collector_classes[collector_name],

name=process_name,

configfile=self.configfile,

handlers=[self.handler_queue]) #初始化对应的Collecter

if collector is None:

self.log.error('Failed to load collector %s',

process_name)

continue

# Splay the loads

time.sleep(float(load_delay))

process = multiprocessing.Process(

name=process_name,

target=collector_process, #对应的Collecter的处理函数

args=(collector, self.metric_queue, self.log)

) #创建对应的子进程,即对应到对应的Collecter-name

process.daemon = True #设置为deamon的形式

process.start() #加载对应的进程

##############################################################

time.sleep(1)

except SIGHUPException: #如果出现了SIGHUPException则进程重新加载配置的操作,然后重新定义信号

# ignore further SIGHUPs for now

original_sighup_handler = signal.getsignal(signal.SIGHUP)

signal.signal(signal.SIGHUP, signal.SIG_IGN)

self.log.info('Reloading state due to HUP')

self.config = load_config(self.configfile)

collectors = load_collectors(

self.config['server']['collectors_path']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成 Python 代码的流程图可以通过以下步骤完成: 1. 确定要绘制流程图的代码逻辑。 2. 安装流程图绘制工具,例如Graphviz。 3. 导入相应的库和模块,例如graphviz库。 4. 创建一个空的流程图对象。 5. 添加节点到流程图中,每个节点代表代码中的一个步骤或操作。 6. 添加边缘连接节点,表示代码执行的顺序。 7. 可选:为节点和边缘添加标签或注释。 8. 保存流程图为图像文件或显示在屏幕上。 以下是一个示例代码,用于生成一个简单的流程图: ```python import graphviz # 创建一个空的流程图对象 flowchart = graphviz.Digraph('Flowchart', format='png') # 添加节点 flowchart.node('A', 'Step A') flowchart.node('B', 'Step B') flowchart.node('C', 'Step C') flowchart.node('D', 'Step D') # 添加边缘连接节点 flowchart.edge('A', 'B') flowchart.edge('B', 'C') flowchart.edge('C', 'D') # 可选:为节点和边缘添加标签或注释 flowchart.node('B', 'Step B\n(Conditional)', shape='diamond') flowchart.edge('B', 'C', label='True') flowchart.edge('B', 'D', label='False') # 保存流程图为图像文件 flowchart.render(filename='flowchart', directory='./', cleanup=True) ``` 运行上述代码后,将生成一个名为`flowchart.png`的流程图文件在当前目录中。 请注意,上述示例代码仅演示了如何使用graphviz库生成简单的流程图。对于更复杂的代码逻辑,可能需要更多的节点和边缘以及适当的注释来准确表示代码的执行流程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值