1. 程式人生 > >Ceilometer: 16、cotyledon多程序除錯

Ceilometer: 16、cotyledon多程序除錯

文章轉自: 

https://blog.csdn.net/mengalong/article/details/81125585

 

Newton版本以前,Ceilometer程式碼除錯方法:

ceilometer在Newton版本以前,polling-agent使用的是oslo_service模組啟動的程序,因此可以直接使用python內建的模組 pdb 直接進行除錯。具體除錯方法如下:

在需要增加斷點的地方,加入一行程式碼:
import pdb;pdb.set_trace()
之後在命令列啟動程序,即可走到斷點處:
/usr/bin/ceilometer-polling --polling-namespaces=central --logfile /var/log/ceilometer.log
進入斷點之後,pdb基本的命令如下:
p var : 列印指定變數var的內容
p  dict_var.__dict__ : 列印dict型別變數的內容
n : 跳過當前行繼續執行下一行
s : 進入當前行對應函式的內部
Newton版本以後,Ceilometer程式碼除錯方法:

ceilometer在Newton版本開始,使用cotyledon 這個模組來啟動程序框架,該模組底層是multiprocess,啟動程序框架之後會進入子程序,此時使用上邊的方法就不能進入到子程序的debug,此時需要使用如下方法:

增加一個新的類,用於除錯多執行緒
import sys
import pdb

class ForkedPdb(pdb.Pdb):
    def interaction(self, *args, **kwargs):
        _stdin = sys.stdin
        try:
            sys.stdin = open('/dev/stdin')
            pdb.Pdb.interaction(self, *args, **kwargs)
        finally:
            sys.stdin = _stdin
在需要新增斷點的地方,使用如下方法新增斷點:
ForkedPdb().set_trace()
之後啟動程序,即可進入到pdb除錯模式,具體的命令和上邊一致
--------------------- 
作者:mengalong 
來源:CSDN 
原文:https://blog.csdn.net/mengalong/article/details/81125585?utm_source=copy 
版權宣告:本文為博主原創文章,轉載請附上博文連結!