Current Usage


In [1]:
import resource
import time

RESOURCES = [
    ('ru_utime', 'User time'),
    ('ru_stime', 'System time'),
    ('ru_maxrss', 'Max. Resident Set Size'),
    ('ru_ixrss', 'Shared Memory Size'),
    ('ru_idrss', 'Unshared Memory Size'),
    ('ru_isrss', 'Stack Size'),
    ('ru_inblock', 'Block inputs'),
    ('ru_oublock', 'Block outputs'),
]

usage = resource.getrusage(resource.RUSAGE_SELF)

for name, desc in RESOURCES:
    print('{:<25} ({:<10}) = {}'.format(
        desc, name, getattr(usage, name)))


User time                 (ru_utime  ) = 0.9065449999999999
System time               (ru_stime  ) = 0.25634999999999997
Max. Resident Set Size    (ru_maxrss ) = 41164800
Shared Memory Size        (ru_ixrss  ) = 0
Unshared Memory Size      (ru_idrss  ) = 0
Stack Size                (ru_isrss  ) = 0
Block inputs              (ru_inblock) = 438
Block outputs             (ru_oublock) = 1

Resources Limits


In [2]:
import resource

LIMITS = [
    ('RLIMIT_CORE', 'core file size'),
    ('RLIMIT_CPU', 'CPU time'),
    ('RLIMIT_FSIZE', 'file size'),
    ('RLIMIT_DATA', 'heap size'),
    ('RLIMIT_STACK', 'stack size'),
    ('RLIMIT_RSS', 'resident set size'),
    ('RLIMIT_NPROC', 'number of processes'),
    ('RLIMIT_NOFILE', 'number of open files'),
    ('RLIMIT_MEMLOCK', 'lockable memory address'),
]

print('Resource limits (soft/hard):')
for name, desc in LIMITS:
    limit_num = getattr(resource, name)
    soft, hard = resource.getrlimit(limit_num)
    print('{:<23} {}/{}'.format(desc, soft, hard))


Resource limits (soft/hard):
core file size          0/9223372036854775807
CPU time                9223372036854775807/9223372036854775807
file size               9223372036854775807/9223372036854775807
heap size               9223372036854775807/9223372036854775807
stack size              8388608/67104768
resident set size       9223372036854775807/9223372036854775807
number of processes     709/1064
number of open files    256/9223372036854775807
lockable memory address 9223372036854775807/9223372036854775807

In [3]:
import resource
import os

soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
print('Soft limit starts as  :', soft)

resource.setrlimit(resource.RLIMIT_NOFILE, (4, hard))

soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
print('Soft limit changed to :', soft)

random = open('/dev/random', 'r')
print('random has fd =', random.fileno())
try:
    null = open('/dev/null', 'w')
except IOError as err:
    print(err)
else:
    print('null has fd =', null.fileno())


ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

Soft limit starts as  : 256
Soft limit changed to : 4
Traceback (most recent call last):
  File "/Users/gaufung/anaconda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
  File "<ipython-input-3-c5d94b513c7e>", line 12, in <module>
    random = open('/dev/random', 'r')
OSError: [Errno 24] Too many open files: '/dev/random'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/gaufung/anaconda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 1821, in showtraceback
AttributeError: 'OSError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/gaufung/anaconda/lib/python3.6/site-packages/IPython/core/ultratb.py", line 1132, in get_records
  File "/Users/gaufung/anaconda/lib/python3.6/site-packages/IPython/core/ultratb.py", line 313, in wrapped
  File "/Users/gaufung/anaconda/lib/python3.6/site-packages/IPython/core/ultratb.py", line 358, in _fixed_getinnerframes
  File "/Users/gaufung/anaconda/lib/python3.6/inspect.py", line 1454, in getinnerframes
  File "/Users/gaufung/anaconda/lib/python3.6/inspect.py", line 1411, in getframeinfo
  File "/Users/gaufung/anaconda/lib/python3.6/inspect.py", line 666, in getsourcefile
  File "/Users/gaufung/anaconda/lib/python3.6/inspect.py", line 695, in getmodule
  File "/Users/gaufung/anaconda/lib/python3.6/inspect.py", line 679, in getabsfile
  File "/Users/gaufung/anaconda/lib/python3.6/posixpath.py", line 374, in abspath
OSError: [Errno 24] Too many open files
---------------------------------------------------------------------------