Skip to content

Instantly share code, notes, and snippets.

@justinfx
Last active March 8, 2024 18:33
Show Gist options
  • 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
@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