Subprocess: Execute two or more preexec_fn
Question: I was wondering if there is a way of creating a
[subprocess](http://docs.python.org/2/library/subprocess.html) (through
`subprocess.Popen`) that calls secuentially two (or more) `preexec_fn`.

For instance, calling `setegid` and `seteuid` (just for example purposes).

So far, I found this workaround (and well... it works, but it doesn't look
too... direct or "clean")

    
    
    #!/usr/bin/python2.7
    
    import subprocess
    import os
    
    def preExecuter(listOfFunctions):
        for functionEntry in listOfFunctions:
            functionEntry["function"](* functionEntry.get("args", []), **functionEntry.get("kwargs", {}))
    
    
    listOfFunctions = [
        {
            "function": os.setegid,
            "args": [1000],
        },
        {
            "function": os.seteuid,
            "args": [1000],
        },
    ]
    
    if __name__ == "__main__":
        sp = subprocess.Popen(["whoami"], preexec_fn=preExecuter(listOfFunctions))
        sp.communicate()
    

Is there a better way of doing this? Thank you in advance.


Answer: That's not even going to work, since the call to preExecuter actually executes
the setegid and seteuid operations, and is done before Popen is called. What's
wrong with

    
    
    def my_pre_exec() :
        os.setegid(1000)
        os.seteuid(1000)
    
    
    subprocess.Popen( ..., preexec_fn = my_pre_exec )
    

