Hi Guys,
I am kind of new to python and started working on python from February2013.
I have a script which run on linux machine. It captures the available RAM from the system and then tries to eat it up. I am using subprocess.Popen('free',...) command and for first loop it works fine, but for second loop it throws an exception. Any clue on this. Code is below:
def get_Ram_avialable():
try:
print 'a1'
process = Popen('free\n', stdout = PIPE, stderr = PIPE, shell = True)
print 'a2'
write_file = open('dump.txt' , 'w')
write_file.write(process)
write_file.close()
time.sleep(2)
read_file = open('dump.txt','r')
if 'Mem:' in line:
raw_data = line.split()
used_bytes = raw_data[2]
free_K_bytes = raw_data[3]
print "\nMemory Used bytes :"+used_bytes,
print ", Free bytes :"+ free_K_bytes
read_file.close()
free_bytes = int(free_K_bytes)*1024
free_bytes = int(free_bytes)*(0.90) # Takes 90% of available RAM
os.remove('dump.txt')
return int(free_bytes)
except Exception, e:
raise Exception("Exception while calculating the RAM")
while True:
fill_space = ' '*get_Ram_avialable()
time.sleep(15)
Output is like this:
a1
a2
Memory Used bytes :70856 , Free bytes :54868
a1
Traceback (most recent call last):
File "./fillRAM.py", line 30, in <module>
fill_space = ' '*get_Ram_avialable()
File "./fillRAM.py", line 23, in get_Ram_avialable
raise Exception("Exception while calculating the RAM")
Exception: Exception while calculating the RAM