I'm trying to make python script to print top 5 processes by cpu/memory usage every minute. However, the cpu result doesn't seem to change when it loops.
How can I get new set of measurements for cpu when it loops?
My code is below.
Thank you for your help!
import psutil
import time;
from functools import cmp_to_key
def log(line):
print(line)
with open("log.txt", "a") as f:
f.write("{}\n".format(line))
def cmpCpu(a, b):
a = a['cpu']
b = b['cpu']
if a > b:
return -1
elif a == b:
return 0
else:
return 1
def cmpMemory(a, b):
a = a['memory']
b = b['memory']
if a > b:
return -1
elif a == b:
return 0
else:
return 1
def getInfo(pid):
p = psutil.Process(pid)
name = p.name()
cpu = p.cpu_percent()
memory = int(p.memory_info().rss/1024/1024)
return {'name':name, 'cpu':cpu, 'memory':memory}
while True:
localtime = time.localtime(time.time())
timestamp = str(localtime.tm_hour)+":"+str(localtime.tm_min)
log(timestamp)
processes = []
for i in psutil.pids():
processes.append(getInfo(i))
#Sort by cpu usage
processes.sort(key=cmp_to_key(cmpCpu))
for i in range(5):
info = processes[i]
info = info['name']+", "+str(info['cpu'])+"%"
log(info)
#Sort by memory usage
processes.sort(key=cmp_to_key(cmpMemory))
for i in range(5):
info = processes[i]
info = info['name']+", "+str(info['memory'])+"MB"
log(info)
time.sleep(60)
It seems like psutil.process_iter is the answer. The code below works.
import psutil
import time;
from functools import cmp_to_key
def log(line):
print(line)
with open("log.txt", "a") as f:
f.write("{}\n".format(line))
def cmpCpu(a, b):
a = a['cpu']
b = b['cpu']
if a > b:
return -1
elif a == b:
return 0
else:
return 1
def cmpMemory(a, b):
a = a['memory']
b = b['memory']
if a > b:
return -1
elif a == b:
return 0
else:
return 1
while True:
localtime = time.localtime(time.time())
timestamp = str(localtime.tm_hour)+":"+str(localtime.tm_min)
log(timestamp)
#Collect information for each process
processes = []
for proc in psutil.process_iter(attrs=['name', 'cpu_percent', 'memory_info']):
processes.append({'name': proc.info['name'], 'cpu': proc.info['cpu_percent'], 'memory': int(proc.info['memory_info'].rss/1024/1024)})
#Sort by cpu usage
log("CPU:")
processes.sort(key=cmp_to_key(cmpCpu))
for i in range(5):
info = processes[i]
info = info['name']+", "+str(info['cpu'])+"%"
log(info)
#Sort by memory usage
log("Memory:")
processes.sort(key=cmp_to_key(cmpMemory))
for i in range(5):
info = processes[i]
info = info['name']+", "+str(info['memory'])+"MB"
log(info)
time.sleep(60)
Related
I want to print 'double' when x equals 'ok' two times consecutively in the while loop.
My script below:
import random
import time
a = 5
while True:
b = random.randint(0, 10)
print(b)
if a > b:
x = 'ok'
print(x)
You need to track your state.
import random
import time
a = 5
prev = False
while True:
b = random.randint(0, 10)
print(b)
if a > b:
if prev:
print('double')
x = 'ok'
prev = True
else:
x = 'ko'
prev = False
This answer is for a triple version of my question:
import random
import time
a = 5
i=0
rec=[]
while True:
b = random.randint(0, 10)
print(b)
if a > b:
x = 'ok'
# print(x)
rec.append(str(x))
i=i+1
else:
x='ko'
# print(x)
rec.append(str(x))
i=i+1
if i>=3 and (rec[i-1]=='ok' and rec[i-2]=='ok' and rec[i-3]=='ok'):
print('trible')
time.sleep(1)
I am writing a scheduling algorithm for testing purposes and following the "Introduction to Algorithm book", but this is as far as I can get. As of now the job is dying in the weed "Endless loop somewhere". Generally, the algorithm should have a profit and we sort all other arrays according to profit "But I am just ignoring that for now" and my inputs are already sorted. So, the deadline and jobs passed are already sorted. Need somehelp as its still not working.
#!/usr/bin/python3.6
class Scheduling:
def schedule(self, n, deadline, jobs):
self.fdeadline = deadline
self.J = []
self.J.append(jobs[0])
self.i = 1
while self.i <= n:
self.K = self.J.copy()
self.K.append(jobs[self.i])
self.i = self.i + 1
if self.feasible(self.K, self.fdeadline) == True :
self.J = self.K
return self.J
def feasible(self, K, fdl):
self.tmp = K
self.isFeasible = True
self.i = 0
self.j = 1
self.k = 0
while self.i < len(self.tmp):
while self.j < len(self.tmp):
self.index1 = self.i
self.index2 = self.j
if (fdl[self.index1] > fdl[self.index2]):
self.tmp[i], self.tmp[j] = self.tmp[j], self.tmp[i]
while self.k < len(self.tmp):
self.job = self.tmp[self.k]
if (fdl[self.job] < k + 1):
isFeasible = False
break
return isFeasible
def main():
sins = Scheduling()
n = 4
deadline = [1,1,2,2]
jobs = [4, 2, 1, 3]
sjobs = sins.schedule(n, deadline, jobs)
print (sjobs)
if __name__ == "__main__":
main()
Here is a fully functioning code to the scheduling, it might need some small refinements, but its working fine.
#!/usr/bin/python3.6
from operator import itemgetter
class Scheduling:
def __init__(self, jobs):
self.jobs = jobs
def schedule(self, n, deadline):
self.sdl = deadline
self.J = []
self.J.append(self.jobs[1])
self.x = 2
while self.x < n:
self.K = self.J.copy()
self.K.append(self.jobs[self.x])
self.x = self.x + 1
self.feasibility = self.feasible(self.K, self.sdl)
if self.feasibility == True:
self.J = self.K.copy()
return self.J
def feasible(self, K, fdl):
self.tmp = K
self.isFeasible = True
self.i = 0
self.j = 1
self.k = 0
while self.i < len(self.tmp):
while self.j < len(self.tmp):
self.index1 = self.jobs.index(self.tmp[self.i])
self.index2 = self.jobs.index(self.tmp[self.j])
self.j = self.j + 1
if (fdl[self.index1] > fdl[self.index2]):
self.tmp[self.i], self.tmp[self.j] = self.tmp[self.j], self.tmp[self.i]
self.i = self.i + 1
while self.k < len(self.tmp):
self.job = self.tmp[self.k]
self.jobindex = self.jobs.index(self.job)
self.dlineval = fdl[self.jobindex]
self.ftest = self.k + 1
self.k = self.k + 1
if (self.dlineval < self.ftest):
self.isFeasible = False
break
return self.isFeasible
def main():
n = 7
jobs = [0, 1, 2, 3, 4, 5, 6]
deadline = [0, 2, 4, 3, 3, 2, 1]
profit = [0 , 46, 52, 30, 36 ,56, 40]
midresult = [list(x) for x in zip(deadline, profit ,jobs)]
midresult.sort(key=lambda k: (k[0], -k[1]))
deadline, profit, jobs = map(list, zip(*midresult))
sins = Scheduling(jobs)
sjobs = sins.schedule(n, deadline)
print("\n Jobs", sjobs)
finalprofit = []
finaldl = []
for c in range(len(sjobs)):
item = sjobs[c]
jobsindex = jobs.index(item)
finalprofit.append(profit[jobsindex])
finaldl.append(deadline[jobsindex])
print("\n profit", finalprofit)
print("\n Deadline", finaldl)
if __name__ == "__main__":
main()
First of all understand self in python. Read link
Secondly understand the job sequencing problem. Read link
Then, look at the below code
class Scheduling:
def schedule(self, n, deadline, jobs):
# max number of jobs you can schedule is the max deadline available.
filledJobs = ['dummy']*max(deadline);
i = 0
# start assigning the jobs in a greeedy way
while i < n:
job = jobs[i]
j = deadline[i]
# assign the job from the last deadline
while j > 0:
if(filledJobs[j-1] == 'dummy'):
filledJobs[j-1] = job
break
j = j - 1
i = i + 1
return filledJobs
def main():
sins = Scheduling()
n = 4
deadline = [1,1,2,2]
# assuming jobs are sorted w.r.t profits
# I represented the jobs with string to be clear
jobs = ['a', 'b', 'c', 'd']
sjobs = sins.schedule(n, deadline, jobs)
print (sjobs)
if __name__ == "__main__":
main()
I am solving a puzzle (Finding if there exists an input for a given automata for which no matter what the starting state is, final state would be same everytime) and have written following python code. A few testcases are written in check method in the code. For these cases program is running fairly fast. However, for testcases where 50 lists(nodes) are present, the programis taking forever to execute. I am storing intermediate results to use further as well. Can someone please review the code and give suggestions on how to increase the performance of the code?
from itertools import product
from copy import deepcopy
class Node:
def __init__(self,id):
self.id = id
self.dict = {}
def __str__(self):
return str(id) + " : " + str(self.dict)
def __repr__(self):
return str(id) + " : " + str(self.dict)
def tryDelete(nodes,_len):
for id in nodes:
y = deepcopy(nodes)
x = y[id]
del y[id]
for id,node in y.items():
for input,result in node.dict.items():
if result == x:
if x.dict[input] == x:
node.dict[input] = node
else:
node.dict[input] = x.dict[input]
pass
if pathPossible(y,_len ,False) == -1:
return x.id
return -2
target = {}
def FindTarget(node,p):
if len(p) == 1:
return node.dict[p[0]]
if node not in target or p not in target[node]:
x = FindTarget(node,p[:-1]).dict[p[-1]]
if node not in target:
target[node] = {}
target[node][p] = x
return target[node][p]
def allSatisy(nodes,p):
x = None
for key,node in nodes.items():
if x is None:
x = FindTarget(node,p)
elif FindTarget(node,p) != x:
return False
return True
def allPossiblePaths(l,n):
#x = int(((l+1)*(l+2))/2)
for i in range(1, n+1):
for p in product(range(l),repeat=i):
yield p
def pathPossible(nodes,_len ,isItereate=True):
i = 1
isFound = False
for p in allPossiblePaths(_len,len(nodes)):
if allSatisy(nodes,p):
isFound = True
break
if isFound:
return -1
elif not isItereate:
return -2
else:
return tryDelete(nodes,_len)
def answer(li):
nodes = {}
for i in range(len(li)):
nodes[i] = Node(i)
for i in range(len(li)):
for j in range(len(li[i])):
nodes[i].dict[j] = nodes[li[i][j]]
return pathPossible(nodes,len(nodes[0].dict))
def check(li,ans):
# each item in the list is a node, each item i-th in the inner list tells to what node the transition happens for input i
x = answer(li)
print(str(li) + " : " + str(ans) + " : " + str(x))
def main():
check([[2,1],[2,0],[3,1],[1,0]],-1)
check([[1,2],[1,1],[2,2]],1)
check([[1,3,0],[1,0,2],[1,1,2],[3,3,3]],-1)
if __name__ == '__main__':
main()
UPDATE: I have done few code changes, but still this needs some review from you guys. Changed code:
from itertools import product
from copy import deepcopy
class Node:
def __init__(self,id):
self.id = id
self.dict = {}
def __str__(self):
return str(self.id) + " : " + str(self.dict)
def __repr__(self):
return str(self.id) + " : " + str(self.dict)
def tryDelete(nodes,_len):
for i in range(len(nodes)):
y = nodes[:]
x = y[i]
del y[i]
tNodes = []
for node in y:
for input,result in node.dict.items():
if result == x:
node.tDict = deepcopy(node.dict)
if x.dict[input] == x.id:
node.dict[input] = node
else:
node.dict[input] = x.dict[input]
if pathPossible(y,_len ,False) == -1:
return x.id
for n in tNodes:
n.dict = n.tDict
del n.tDict
return -2
target = {}
def FindTarget(node,p):
if len(p) == 1:
return node.dict[p[0]]
if node not in target or p not in target[node]:
x = Gnodes[FindTarget(node,p[:-1])].dict[p[-1]]
if node not in target:
target[node] = {}
target[node][p] = x
return target[node][p]
def allSatisy(nodes,p):
x = None
for node in nodes:
if x is None:
x = FindTarget(node,p)
elif FindTarget(node,p) != x:
return False
return True
def allPossiblePaths(l,n):
#x = int(((l+1)*(l+2))/2)
for i in range(1, n + 1):
for p in product(range(l),repeat=i):
yield p
def pathPossible(nodes,_len ,isItereate=True):
i = 1
isFound = False
for p in allPossiblePaths(_len,len(nodes)):
if allSatisy(nodes,p):
isFound = True
break
if isFound:
return -1
elif not isItereate:
return -2
else:
return tryDelete(nodes,_len)
Gnodes = []
def answer(li):
Gnodes[:] = []
for i in range(len(li)):
Gnodes.append(Node(i))#[i] = Node(i)
for i in range(len(li)):
for j in range(len(li[i])):
Gnodes[i].dict[j] = li[i][j]
return pathPossible(Gnodes,len(Gnodes[0].dict))
def check(li,ans):
x = answer(li)
print(str(li) + " : " + str(ans) + " : " + str(x))
def main():
check([[2,1],[2,0],[3,1],[1,0]],-1)
check([[1,2],[1,1],[2,2]],1)
check([[1,3,0],[1,0,2],[1,1,2],[3,3,3]],-1)
if __name__ == '__main__':
main()
There is a wonderful graph library called NetworkX. It deals with creating graphs and path finding. You specify what edges or paths exist in your Graph and you can find paths using a plethora of algorithms like breadth first search, or A*, and many others in the algorithms section. The best way to optimize your time is code reuse.
https://networkx.github.io
Reading the documentation: https://docs.python.org/2/library/multiprocessing.html
I decided to write a cpu intensive code and compare multiprocessing with serial computation.
First of all, if this library is using multiprocessing, then why I only see 1 python.exe process?
Secondly, why serial computation takes 12 seconds while multiprocessed one takes 22 seconds?
serial code:
from datetime import datetime
def calc_fib(ind):
fb = 1
if ind >= 3:
prev = 1
i = 2
while i < ind:
prev_tmp = fb
fb += prev
prev = prev_tmp
i += 1
return fb
def long_calc_fib(ind):
val = 0
for j in range(500):
val = calc_fib(ind)
return val
if __name__ == "__main__":
t1 = datetime.now()
for i in range(10):
tmp = long_calc_fib(10000)
t2 = datetime.now()
print str(t2 - t1)
multiprocessing pool code:
from datetime import datetime
from multiprocessing.pool import ThreadPool
def calc_fib(ind):
fb = 1
if ind >= 3:
prev = 1
i = 2
while i < ind:
prev_tmp = fb
fb += prev
prev = prev_tmp
i += 1
return fb
def long_calc_fib(ind):
val = 0
for j in range(500):
val = calc_fib(ind)
return val
if __name__ == "__main__":
t1 = datetime.now()
pool = ThreadPool(processes=10)
async_results = []
for i in range(10):
async_results.append(pool.apply_async(long_calc_fib, (10000,)))
for res in async_results:
tmp = res.get()
t2 = datetime.now()
print str(t2 - t1)
My mistake.
I must have used Pool instead of ThreadPool.
By chaning ThreadPool to Pool, I reduced the time to 3 seconds.
The following script is supposed to fetch a specific line number and parse it from a live website. It works for like 30 loops but then it seems like enumerate(f) stops working correctly... the "i" in the for loop seems to stop at line 130 instead of like 200 something. Could this be due to the website I'm trying to fetch data from or something else? Thanks!!
import sgmllib
class MyParser(sgmllib.SGMLParser):
"A simple parser class."
def parse(self, s):
"Parse the given string 's'."
self.feed(s)
self.close()
def __init__(self, verbose=0):
"Initialise an object, passing 'verbose' to the superclass."
sgmllib.SGMLParser.__init__(self, verbose)
self.divs = []
self.descriptions = []
self.inside_div_element = 0
def start_div(self, attributes):
"Process a hyperlink and its 'attributes'."
for name, value in attributes:
if name == "id":
self.divs.append(value)
self.inside_div_element = 1
def end_div(self):
"Record the end of a hyperlink."
self.inside_div_element = 0
def handle_data(self, data):
"Handle the textual 'data'."
if self.inside_div_element:
self.descriptions.append(data)
def get_div(self):
"Return the list of hyperlinks."
return self.divs
def get_descriptions(self, check):
"Return a list of descriptions."
if check == 1:
self.descriptions.pop(0)
return self.descriptions
def rm_descriptions(self):
"Remove all descriptions."
self.descriptions.pop()
import urllib
import linecache
import sgmllib
tempLine = ""
tempStr = " "
tempStr2 = ""
myparser = MyParser()
count = 0
user = ['']
oldUser = ['none']
oldoldUser = [' ']
array = [" ", 0]
index = 0
found = 0
k = 0
j = 0
posIndex = 0
a = 0
firstCheck = 0
fCheck = 0
while a < 1000:
print a
f = urllib.urlopen("SITE")
a = a+1
for i, line in enumerate(f):
if i == 187:
print i
tempLine = line
print line
myparser.parse(line)
if fCheck == 1:
result = oldUser[0] is oldUser[1]
u1 = oldUser[0]
u2 = oldUser[1]
tempStr = oldUser[1]
if u1 == u2:
result = 1
else:
result = user is oldUser
fCheck = 1
user = myparser.get_descriptions(firstCheck)
tempStr = user[0]
firstCheck = 1
if result:
array[index+1] = array[index+1] +0
else:
j = 0
for z in array:
k = j+2
tempStr2 = user[0]
if k < len(array) and tempStr2 == array[k]:
array[j+3] = array[j+3] + 1
index = j+2
found = 1
break
j = j+1
if found == 0:
array.append(tempStr)
array.append(0)
oldUser = user
found = 0
print array
elif i > 200:
print "HERE"
break
print array
f.close()
Perhaps the number of lines on that web page are fewer than you think? What does this give you?:
print max(i for i, _ in enumerate(urllib.urlopen("SITE")))
Aside: Your indentation is stuffed after the while a < 1000: line. Excessive empty lines and one-letter names don't assist the understanding of your code.
enumerate is not broken. Instead of such speculation, inspect your data. Suggestion: replace
for i, line in enumerate(f):
by
lines = list(f)
print "=== a=%d linecount=%d === % (a, len(lines))
for i, line in enumerate(lines):
print " a=%d i=%d line=%r" % (a, i, line)
Examine the output carefully.