Skip to content

Instantly share code, notes, and snippets.

@justinfx
Last active March 8, 2024 18:33
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinfx/4741527 to your computer and use it in GitHub Desktop.
Save justinfx/4741527 to your computer and use it in GitHub Desktop.
An example of how to use multiprocessing from within Maya, by actually running it as a subprocess and communicating back the results.
"""
This code can be run from a Maya script editor
"""
import subprocess
import cPickle
print 'This value below should be a 1000:'
p = subprocess.Popen(["/path/to/multi_test.py", "-po"], stdout=subprocess.PIPE)
result = cPickle.load(p.stdout)
print result
#!/usr/bin/env python
import multiprocessing
from time import time
import cPickle
import sys
## Simple func to eat up cpu power.
def whileFunc(z):
while z < 100000:
z += 1
return z
if __name__ == "__main__":
## Get current time
currtime = time()
## How often to run (just a test value)
N = 1000
## Just a list with 1s
myList = [1]*N
nrOfProcessors = multiprocessing.cpu_count()
## Set our pool of processors
po = multiprocessing.Pool(nrOfProcessors)
## create the threads
res = po.map_async(whileFunc, myList)
## If we pass a -po flag, pickle the output and write it out
if '-po' in sys.argv[1:]:
results = len(res.get())
cPickle.dump(results, sys.stdout, -1)
sys.stdout.flush()
sys.exit(0)
print 'This value below should be a 1000:'
print len(res.get())
print 'time elapsed:', time() - currtime
@akashCGI
Copy link

hi, When i tried running this in Maya 2015, I got the following error, Any Idea what could be going wrong ?:
"""
This code can be run from a Maya script editor
"""
import subprocess
import cPickle

print 'This value below should be a 1000:'

p = subprocess.Popen(["C:/test/multi_test.py", "-po"], stdout=subprocess.PIPE)
result = cPickle.load(p.stdout)

print result
This value below should be a 1000:

Error: 193

Traceback (most recent call last):

File "", line 9, in

File "C:\Program Files\Autodesk\Maya2015\bin\python27.zip\subprocess.py", line 679, in init

errread, errwrite)

File "C:\Program Files\Autodesk\Maya2015\bin\python27.zip\subprocess.py", line 896, in _execute_child

startupinfo)

WindowsError: [Error 193] %1 is not a valid Win32 application

@jf20
Copy link

jf20 commented Nov 19, 2019

HI Justtin
Get the same 193 error as above

WindowsError: [Error 193] %1 is not a valid Win32 application

This value below should be a 1000:

Error: 193

Traceback (most recent call last):

File "", line 9, in

File "C:\Program Files\Autodesk\Maya2018\bin\python27.zip\subprocess.py", line 710, in init

errread, errwrite)

File "C:\Program Files\Autodesk\Maya2018\bin\python27.zip\subprocess.py", line 958, in _execute_child

startupinfo)

WindowsError: [Error 193] %1 is not a valid Win32 application

Is there a fix for this error. Any help would be appreciated.

Thanks

@justinfx
Copy link
Author

justinfx commented Nov 19, 2019

@jf20 / @akashCGI

I don't have a windows box to test, but it looks like it is caused by the translation of my linux/osx code to a Windows command, but not accounting for the difference that windows actually needs the path to the python executable as the first arguments.

Untested, but you might need to do something like

import sys

p = subprocess.Popen([sys.executable, "C:/test/multi_test.py", "-po"], stdout=subprocess.PIPE)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment