Question

I've been looking at examples from other people but I can't seem to get it to work properly.
It'll either use a single core, or basically freeze up maya if given too much to process, but I never seem to get more than one core working at once.

So for example, this is kind of what I'd like it to do, on a very basic level. Mainly just let each loop run simultaneously on a different processor with the different values (in this case, the two values would use two processors)

mylist = [50, 100, 23]

newvalue = [50,51]

for j in range(0, len(newvalue)):

    exists = False
    for i in range(0, len(mylist)):

        #search list
        if newvalue[j] == mylist[i]:
            exists = True

    #add to list
    if exists == True:
        mylist.append(mylist)

Would it be possible to pull this off? The actual code I'm wanting to use it on can take from a few seconds to like 10 minutes for each loop, but they could theoretically all run at once, so I thought multithreading would speed it up loads

Bear in mind I'm still relatively new to python so an example would be really appreciated

Cheers :)

Was it helpful?

Solution

There are really two different answers to this.

Maya scripts are really supposed to run in the main UI thread, and there are lots of ways they can trip you up if run from a separate thread. Maya includes a module called maya.utils which includes methods for deferred evaluation in the main thread. Here's a simple example:

import maya.cmds as cmds
import maya.utils as utils
import threading

def do_in_main():
    utils.executeDeferred (cmds.sphere)

for i in range(10):
    t  = threading.Thread(target=do_in_main, args=())
    t.start()

That will allow you to do things with the maya ui from a separate thread (there's another method in utils that will allow the calling thread to await a response too). Here's a link to the maya documentation on this module

However, this doesn't get you around the second aspect of the question. Maya python isn't going to split up the job among processors for you: threading will let you create separate threads but they all share the same python intepreter and the global interpreter lock will mean that they end up waiting for it rather than running along independently.

You can't use the multiprocessing module, at least not AFAIK, since it spawns new mayas rather than pushing script execution out into other processors in the Maya you are running within. Python aside, Maya is an old program and not very multi-core oriented in any case. Try XSI :)

Any threading stuff in Maya is tricky in any case - if you touch the main application (basically, any function from the API or a maya.whatever module) without the deferred execution above, you'll probably crash maya. Only use it if you have to.

And, BTW, you cant use executeDeferred, etc in batch mode since they are implemented using the main UI loop.

OTHER TIPS

What theodox says is still true today, six years later. However one may go another route by spawning a new process by using the subprocess module. You'll have to communicate and share data via sockets or something similar since the new process is in a seperate interpreter. The new interpreter runs on its own and doesn't know about Maya but you can do any other work in it benefitting from the multi-threaded environment your OS provides before communicating it back to your Maya python script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top