0

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.

Leo
  • 222
  • 1
  • 4
  • 15

1 Answers1

2

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)
Jackal
  • 195
  • 9
  • I suppose this would before or after my function. If so, this would be of no use, because this very function causes the problem and no loop around it or so – Leo Apr 02 '20 at 15:26
  • 1
    You could simply start it with a thread before calling your function, like so: ```threading.Thread(name="Memory Regulator", target=checkMemory()).start()``` This would cause the function to run continuously. – Jackal Apr 02 '20 at 15:35