title: How do I automatically kill a process that uses too much memory with Python?
body: <p><strong>The situation:</strong> I have a website that allows people to execute arbitrary code <em>in a different language</em> (specifically, an esolang I created), using a Python interpreter on a shared-hosting server. I run this code in a separate process which is given a time limit of 60 seconds.</p>
  
  <p><strong>The problem:</strong> You can do stuff like (Python equivalent) <code>10**(10**10)</code>, which rapidly consumes far more memory than I have allotted to me. It also, apparently, locks up Apache - or it takes too long to respond - so I have to restart it.</p>
  
  <p>I have seen <a href="https://stackoverflow.com/questions/187804/automatically-kill-process-that-consume-too-much-memory-or-stall-on-linux">this question</a>, but the given answer uses Perl, which I do not know at all, hence I'd like an answer in Python. The OS <em>is</em> Linux too, though.</p>
  
  <p>Specifically, I want the following characteristics:</p>
  
  <ol>
  <li>Runs automatically</li>
  <li>Force-kills any process that exceeds some memory limit like 1MB or 100MB</li>
  <li>Kills any process spawned by my code that is more than 24 hours old</li>
  </ol>
  
  <p>I use this piece of code (in a Django view) to create the process and run it (<code>proxy_prgm</code> is a Manager so I can retrieve data from the program that's interpreting the esolang code):</p>
  
  <pre><code>prgmT[uid] = multiprocessing.Process(
      target = proxy_prgm.runCatch,
      args = (steps,),
      name="program run")
  
  prgmT[uid].start()
  prgmT[uid].join(60) #time limit of 1 minute
  
  if prgmT[uid].is_alive():
      prgmT[uid].terminate()
      proxy_prgm.stop()
  </code></pre>
  
  <p>If you need more details, don't hesitate to tell me what to edit in (or ask me questions).</p>