Python subprocess.Popen() questions

Hey guys,

I’m trying to use subprocess to gather some information from one of our tools that needs to be run via cmd line within our shell.

I am running subprocess.Popen() from within a script called within Maya to do this.

Maya is running within our shell, that is setup via batch at Maya launch.



gameRoot=os.environ.get("GAME_ROOT") + '\	ools\\options'
_cmd=[r'python', 'import os', 'os.environ.copy()']
		
task = subprocess.Popen(_cmd, cwd=gameRoot, stdout=subprocess.PIPE) #shell=True,
task.wait()	


I am finding as soon as I introduce the subprocess.PIPE to the mix I get a funky crash. Obviously I need the Pipe to retrieve the data I want.

Error: (6, ‘The handle is invalid’)

Traceback (most recent call last):

File “<maya console>”, line 1, in <module>

File “…\maya\2009\python\ssgtools\utils\ssgSubProc.py”, line 10, in ssgSubProcGetEnv

task = subprocess.Popen(_cmd, cwd=gameRoot, stdout=subprocess.PIPE) #shell=True,

File “C:\Program Files (x86)\Autodesk\Maya2009\bin\python25.zip\subprocess.py”, line 586, in init

errread, errwrite) = self._get_handles(stdin, stdout, stderr)

File “C:\Program Files (x86)\Autodesk\Maya2009\bin\python25.zip\subprocess.py”, line 699, in _get_handles

p2cread = self._make_inheritable(p2cread)

File “C:\Program Files (x86)\Autodesk\Maya2009\bin\python25.zip\subprocess.py”, line 744, in _make_inheritable

DUPLICATE_SAME_ACCESS)

WindowsError: (6, ‘The handle is invalid’)

My solution is to write the data to a file on disk as I have this option with the exe I need to use, then read that files contents in and use that, but it kinda defeats the point of being able to run a sub process and create a temp file using temp file then pull the data from that container.

any ideas? Its driving me nuts…

Try capturing both stdout and stderr. Also try communicate:

stdout, stderr = subprocess.Popen(_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

You cannot use PIPE’s from within Maya. It sucks so bad. It is a known python bug when using Popen through some GUI apps.

So, yes, figure out some workaround like you have.

Use sockets to communicate with your tools, you can write in any language then.

You cannot use PIPE's from within Maya. It sucks so bad. It is a known python bug when using Popen through some GUI apps.

We do it a fair bit. Mind you, we are using Maya 2009 python still, perhaps something broke down the line.

Autodesk modifies the standard subprocess module, and I had run into an issue in Maya 2011 where that would cause pipes to fail (Non file-like object error). Luckily they include the originals in the python26.zip file. It’s called: subprocess_original, just import it instead of subprocess. (Although I don’t think this is your issue here).

Also as Rob mentioned, typically the DUPLICATE_SAME_ACCESS error is related to only passing a PIPE as either stdin or stdout but not both. Try using them as both even if you don’t need it.

Workaround on Windows:


# command_string is my command with any arguments
windows_wrapper_string = 'start ' + command_string
os.system( windows_wrapper_string )