import os
import subprocess


def cmd_output(args, **kwds):
    kwds.setdefault("stdout", subprocess.PIPE)
    kwds.setdefault("stderr", subprocess.STDOUT)
    p = subprocess.Popen(args, **kwds)
    return p.communicate()[0]

def memUsage():
    """
    Return current memory usage of this process in megabytes,
    according to the RSS column of the output of ps v.
    """
    pid = os.getpid()
    cmd = "ps v %d" % (pid)
    psOutput = cmd_output(cmd.split())
    return int(psOutput.split()[17])/1024.
