简体   繁体   中英

python break process if memory consumption is to high

I am using a module and sometimes it crashes, because of memory consumption. It is terminated by SIGKILL 9 and the script is interrupted. However I cannot access the loop in which the memory consumption happens.

This is how it looks now:

import module
output = module.function(args)

and this sort of how it should look like:

import module
if ramconsumption(module.function(args)) > 999:
     output = None
else:
     output = module.function(args)

Do you know a way on how to implement this? My example is just for better understanding, the solution should just be one where I do not get a SIGKILL, when too much memory is consumed.

This might work:

import os
import psutil
import module
import threading

def checkMemory():
    while True:
        process = psutil.Process(os.getpid())
        memoryUsage = process.memory_info().rss #in bytes
        if memoryUsage > amount:
            print("Memory budget exceeded")
            break
    os._exit(0)

threading.Thread(name="Memory Regulator", target=checkMemory).start()

output = module.function(args)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM