I need to limit the amount of memory being used when running a Python script in NiFi, and I found this answer in Stack Overflow. I follow the logic of how the code works, but I'm a bit stumped as to how to actually implement it in a Python script.
For example, if I have a script
import sys
import json
flowFile = sys.stdin.read()
try:
j = json.loads(flowFile)
sys.stdout.write(j)
except:
sys.stdout.write(flowFile)
would I then implement the memory limit as
import sys
import json
def memory_limit():
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
resource.setrlimit(resource.RLIMIT_AS, (get_memory() * 1024 / 2, 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
if __name__ == '__main__':
memory_limit() # Limitates maximun memory usage to half
try:
main()
except MemoryError:
sys.stderr.write('\n\nERROR: Memory Exception\n')
sys.exit(1)
flowFile = sys.stdin.read()
try:
j = json.loads(flowFile)
sys.stdout.write(j)
except:
sys.stdout.write(flowFile)
or something else entirely?