3 __author__ =
'Ali GOREN <goren.ali@yandex.com>'
4 __repo__ =
'https://github.com/aligoren/syspy'
5 __license__ =
'Apache v2.0 License'
25 http://stackoverflow.com/a/10058239/3821823
26 dict.items would return an iterable dict view object rather than a list.
27 We need to wrap the call onto a list in order to make the indexing possible
34 >>> a = psutil.cpu_times()
36 Traceback (most recent call last):
37 File "<stdin>", line 1, in <module>
38 AttributeError: 'scputimes' object has no attribute 'items'
40 Traceback (most recent call last):
41 File "<stdin>", line 1, in <module>
42 AttributeError: 'scputimes' object has no attribute '__dict_'
45 ItemsView(OrderedDict([('user', 822.62451171875), ('system', 373.341796875), ('i
46 dle', 21677.9609375)]))
48 [('user', 822.62451171875), ('system', 373.341796875), ('idle', 21677.9609375)]
49 >>> list(b.items())[0]
50 ('user', 822.62451171875)
51 >>> list(b.items())[0][1]
53 >>> ass = list(b.items())
60 >>> psutil.cpu_times()
61 scputimes(user=934.0092163085938, system=431.310546875, idle=23977.853515625)
68 https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch07s04.html
72 cpuToDict = psutil.cpu_times()
73 cpuDict = cpuToDict.__dict__
74 listCpuValues = list(cpuDict.items())
76 ret_val_user =
"{0}".format(listCpuValues[0][1])
77 ret_val_system =
"{0}".format(listCpuValues[1][1])
78 ret_val_idle =
"{0}".format(listCpuValues[2][1])
80 return ret_val_user, ret_val_system, ret_val_idle
87 >>> psutil.cpu_percent(interval=1)
89 >>> for x in range(6):
90 ... psutil.cpu_percent(interval=1)
101 currentCpuPercent = psutil.cpu_percent(interval=1)
103 return currentCpuPercent
110 >>> for i in range(3):
111 ... psutil.cpu_percent(interval=1,percpu=True)
116 >>> a = psutil.cpu_percent(interval=1,percpu=True)
120 Traceback (most recent call last):
121 File "<stdin>", line 1, in <module>
122 TypeError: 'float' object is not subscriptable
124 Traceback (most recent call last):
125 File "<stdin>", line 1, in <module>
126 TypeError: 'float' object is not subscriptable
133 >>> for i in range(3):
134 ... a = psutil.cpu_percent(interval=1,percpu=True)
137 Traceback (most recent call last):
138 File "<stdin>", line 3, in <module>
139 TypeError: 'float' object is not subscriptable
144 cpuPerCpuPercent = psutil.cpu_percent(interval=1, percpu=
True)
146 return cpuPerCpuPercent
153 >>> for x in range(4):
154 ... psutil.cpu_times_percent(interval=1, percpu=False)
156 scputimes(user=6.9, system=0.0, idle=93.1)
157 scputimes(user=0.0, system=0.9, idle=99.1)
158 scputimes(user=0.0, system=0.0, idle=100.0)
159 scputimes(user=0.0, system=0.0, idle=100.0)
160 >>> for x in range(4):
161 ... psutil.cpu_times_percent(interval=1, percpu=True)
163 [scputimes(user=0.0, system=0.0, idle=100.0), scputimes(user=0.0, system=0.1, id
165 [scputimes(user=0.0, system=1.4, idle=98.6), scputimes(user=0.0, system=0.0, idl
167 [scputimes(user=0.0, system=0.1, idle=99.9), scputimes(user=0.0, system=0.0, idl
169 [scputimes(user=0.0, system=0.0, idle=100.0), scputimes(user=0.0, system=1.5, id
171 >>> a = psutil.cpu_times_percent(interval=1 percpu=False)
172 File "<stdin>", line 1
173 a = psutil.cpu_times_percent(interval=1 percpu=False)
175 SyntaxError: invalid syntax
176 >>> a = psutil.cpu_times_percent(interval=1, percpu=False)
187 Traceback (most recent call last):
188 File "<stdin>", line 1, in <module>
189 AttributeError: 'list' object has no attribute 'items'
190 >>> a = psutil.cpu_times_percent(interval=1, percpu=True)
193 scputimes(user=0.0, system=0.0, idle=100.0)
204 timesPercent = psutil.cpu_times_percent(interval=1, percpu=
False)
205 percentList = list(timesPercent)
207 ret_val_user =
"{0}".format(percentList[0])
208 ret_val_system =
"{0}".format(percentList[1])
209 ret_val_idle =
"{0}".format(percentList[2])
211 return ret_val_user, ret_val_system, ret_val_idle
218 >>> psutil.cpu_count()
220 >>> psutil.cpu_count(logical=False)
222 >>> psutil.cpu_count(logical=True)
228 cpuCount =
"{0}".format(psutil.cpu_count())
229 cpuCountLogicalFalse =
"{0}".format(psutil.cpu_count(logical=
False))
231 return cpuCount, cpuCountLogicalFalse
238 >>> p = psutil.Process()
240 Traceback (most recent call last):
241 File "<stdin>", line 1, in <module>
242 AttributeError: 'Process' object has no attribute 'cpuaffinity'
249 >>> a = list(range(psutil.cpu_count()))
250 >>> p.cpu_affinity(a)
253 >>> p.cpu_affinity(a)
258 cAffinity = psutil.Process()
259 c_affinity =
"{0}".format(cAffinity.cpu_affinity())
def cpuTimesPercent(self)
def cpuPercentPerCpu(self)