大家好,
这是我在python中完成所需的帮助。
稍微解释一下上下文: 我以某个固定的间隔(假设为10秒)重复获取流程相关数据,这是当前存在的所有流程的信息。 (我主要对国家感兴趣)
我需要在python中创建数据结构,基本上为每个进程存储每个状态的总时间,每次我得到样本(比如说每10秒),所以增加状态我在这个时间找到它,如果早期记录存在,或为该州创建新记录等。
因此对于每个进程,我需要在字符串中维护状态,如“S”,“R”,“Z”或“UN”,以及每个状态中的时间(如果在同一个进程中找到进程,则增加相同的状态状态为最后一次,或者如果第一次进程出现在该状态时添加该状态,或者增加它现在处于的状态,如果它之前处于该状态并且我们已经在内部数据结构中创建了该记录)
我有一个双字典,
process_state_info = {}
然后我以10秒的间隔得到我的样本,说3个进程正在运行,处于这样状态,这表明自从最后10秒以来,这些pid处于这种状态,
pid:1, state "S"
pid:2, state "R"
pid 3, state "UN"
所以应该建立这种类型的结构
process_state_info = {1: [{"state":"S", "total_time": 10}],2:
[{"state":"R","total_time":10}],3:[{"state":"UN","total_time":10}]}
每个pid在下一个样本中可能会处于不同的状态,因此我需要将pid映射到所有可能的状态以及在这些状态下花费的时间。
例如,如果下一个样本给我,
pid:1, state "R" ( changed state, so i need to add an entry in inner structure)
pid:2, state "R" ( in same state, so i will increment existing entry by10 seconds)
pid 3, state "Z" (changed state, add entry in inner structure)
pid 4, state "R" (new pid, add new outer structure)
所以process_state_info需要成为
process_state_info = {1: [{"state":"S", "total_time": 10}, {"state":R", "total"time":10}],2:[{"state":"R","total_time":20}],3:[{"state":"UN","total_time":10}, {"state":"Z","total_time":10}], 4:[{"state":"R", "total_time":10}]}
我正在粘贴我正在尝试的代码,以及我遇到的问题,
process_state_info = {}
for process in processes:
found_pid = 0
found_state = 0
print "now process .. "
print process.pid()
for g_pid,g_state_info in process_state_info.items():
if g_pid == process.pid(): #if we found the process already in our data structure
print "found process in our data structure"
found_pid = 1
for state_iter in g_state_info:
print state_iter
if state_iter['state'] == process.s_name():
print "found state for the process in our data structure already"
#add interval to that record
current_total = state_iter['total_time']
state_iter['total_time'] = current_total + self.delta_time
found_state = 1
print "printing process table again"
print process_state_info
break
if found_state == 0:
print "found pid but didnt find state inside, adding new state record"
inner_record = {"total_time":0,"state":""}
inner_record['state'] = process.s_name()
inner_record['total_time'] = delta_time
process_state_info[g_pid].update(inner_record)
print "printing process table now"
print process_state_info
break
if found_pid == 0:
print "didnt find it in our table"
print "printing process table"
print process_state_info
inner_record = {"total_time":0,"state":""}
inner_record['state'] = process.s_name()
inner_record['total_time'] = self.delta_time
g_pid=process.pid()
print "gpid is"
print g_pid
process_state_info[g_pid].update(inner_record) # HOW TO ADD INNER RECORD ?
print "printing process table again after adding new pid record"
我目前得到的输出:
printing process table at start of function print_report
{}
now process ..
1
didnt find it in our table
printing process table
{}
gpid is
1
错误追溯:
Traceback (most recent call last): File "./pcp-pidstat.py", line 756, in <module>
sts = manager.run() File "/usr/lib64/python2.7/site-packages/pcp/pmcc.py", line 665, in run
self._printer.report(self) File "./pcp-pidstat.py", line 733, in report
report.print_report(timestamp, header_indentation, value_indentation) File "./pcp-pidstat.py", line 420, in print_report
process_state_info[g_pid].update(inner_record) # HOW TO ADD INNER RECORD ? KeyError: 1L <--- why does this happen, and why the "L" ?
有什么建议.. python专家?
答案 0 :(得分:0)
在Python 2.x中,long
和int
是不同的数据类型。 1L
是第一个的字面值,表示为long
。数值相等的long
和int
值仍然是单独的dict键。您的process.pid()
来电为您long
,但process_state_info
中的现有密钥为int
。