Limit RAM usage to python program

后端 未结 3 1281
梦如初夏
梦如初夏 2020-11-27 17:19

I\'m trying to limit the RAM usage from a Python program to half so it doesn\'t totally freezes when all the RAM is used, for this I\'m using the following code which is not

3条回答
  •  囚心锁ツ
    2020-11-27 17:47

    I modify the answer of @Ulises CT. Because I think to change too much original function is not so good, so I turn it to a decorator. I hope it helps.

    import resource
    import platform
    import sys
    
    def memory_limit(percentage: float):
        """
        只在linux操作系统起作用
        """
        if platform.system() != "Linux":
            print('Only works on linux!')
            return
        soft, hard = resource.getrlimit(resource.RLIMIT_AS)
        resource.setrlimit(resource.RLIMIT_AS, (get_memory() * 1024 * percentage, hard))
    
    def get_memory():
        with open('/proc/meminfo', 'r') as mem:
            free_memory = 0
            for i in mem:
                sline = i.split()
                if str(sline[0]) in ('MemFree:', 'Buffers:', 'Cached:'):
                    free_memory += int(sline[1])
        return free_memory
    
    def memory(percentage=0.8):
        def decorator(function):
            def wrapper(*args, **kwargs):
                memory_limit(percentage)
                try:
                    function(*args, **kwargs)
                except MemoryError:
                    mem = get_memory() / 1024 /1024
                    print('Remain: %.2f GB' % mem)
                    sys.stderr.write('\n\nERROR: Memory Exception\n')
                    sys.exit(1)
            return wrapper
        return decorator
    
    @memory(percentage=0.8)
    def main():
        print('My memory is limited to 80%.')
    

提交回复
热议问题