You can use Python's resource module to set limits before spawning your subprocess.
For monitoring, resource.getrusage() will give you summarized information over all your subprocesses; if you want to see per-subprocess information, you can do the /proc trick in that other comment (non-portable but effective), or layer a Python program in between every subprocess and figure out some communication (portable, ugly, mildly effective).
Having a PID number of your subprocess you can read all info from proc file-system. Use:
/proc/[PID]/smaps (since Linux 2.6.14) This file shows memory consumption for each of the process's mappings. For each of mappings there is a series of lines as follows:
or
/proc/[PID]/statm Provides information about memory usage, measured in pages.
Alternatively you can limit resources which subprocess can aquire with :
subprocess.Popen('ulimit -v 1024; ls', shell=True)
When given virtual memory limit is reached process fails with out of memory.
精彩评论