Resource usage in python














































Resource usage in python



Resource Usage :

There are  functions which are used to retrieve resource usage information:

resource.getrusage(who):

This function is used to return an object used by current process or its children. It returns different fields. From the fields of return value, we can get information about how resource has been used.

resource.getpagesize():

This method is used to return number of bytes in a system page. It may not be same as the physical page size.

here are some contants related to resource usage:

1)RUSAGE_SELF:

It helps to request resources consumed by calling process. It is sum of the all resources of different threads.

2)RUSAGE_CHILDREN:

It helps to request resources consumed by the child process.

3)RUSAGE_BOTH

It helps to request resources consumed by calling process as well as the child process. It is not available for all systems

4)RUSAGE_THREAD

Resource usage by the current thread. It is not available for all systems


here is an example code:

import resource
resLimits = resource.getrusage(resource.RUSAGE_SELF)
print(resLimits)
print('Page Size: ' + str(resource.getpagesize()))
resource.setrlimit(resource.RLIMIT_CPU, (1, 2))
print('Resouce Limits: ' + str(resource.getrlimit(resource.RLIMIT_CPU)))
for i in range(1000):
for j in range(1000):
for k in range(1000):
pass


Output:

resource.struct_rusage(ru_utime=0.035801, ru_stime=0.01074, ru_maxrss=9356, ru_ixrss=0, ru_idrss=0, 
ru_isrss=0, ru_minflt=1147, ru_majflt=0, ru_nswap=0, ru_inblock=0, ru_oublock=0,
ru_msgsnd=0, ru_msgrcv=0, ru_nsignals=0, ru_nvcsw=0, ru_nivcsw=17)
Page Size: 4096
Resouce Limits: (1, 2)
CPU time limit exceeded (core dumped)

<=Prev






Comments