如何使用python(maya)多线程 [英] How to use python (maya) multithreading

查看:522
本文介绍了如何使用python(maya)多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看别人的例子,但是我似乎无法使其正常工作.
它会使用单个内核,或者如果处理量过多,则基本上冻结了maya,但我似乎从来没有一次使用多个内核.

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)

是否有可能做到这一点?我想在每个循环上使用的实际代码可能需要几秒钟到10分钟左右的时间,但是理论上它们可以一次运行,所以我认为多线程会加快加载速度

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

请记住,我对python还是比较陌生的,所以我会很感激一个例子

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

干杯:)

推荐答案

对此确实有两个不同的答案.

There are really two different answers to this.

Maya脚本实际上应该在主UI线程中运行,如果从单独的线程运行,则有很多方法可以使您崩溃. Maya包含一个名为maya.utils的模块,该模块包括在主线程中进行递延求值的方法.这是一个简单的示例:

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()

这将使您可以从单独的线程处理maya ui(utils中还有另一个方法,该方法也允许调用线程等待响应).这是一个链接至该模块上的maya文档

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

但是,这并不能帮助您解决问题的第二个方面. Maya python不会为您分配处理器之间的工作:threading将允许您创建单独的线程,但是它们都共享相同的python解释器和

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.

您不能使用multiprocessing模块,至少不能使用AFAIK,因为它会产生新的Maya,而不是将脚本执行推入正在运行的Maya中的其他处理器中.除了Python,Maya是一个旧程序,无论如何都不是面向多核的.尝试XSI:)

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 :)

在任何情况下,Maya中的任何线程处理都是棘手的-如果您在没有延迟执行的情况下触摸主应用程序(基本上是API或maya.what模块中的任何函数),则可能会使maya崩溃.仅在必要时使用它.

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.

而且,顺便说一句,您不能在批处理模式下使用executeDeferred等,因为它们是使用主UI循环实现的.

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

这篇关于如何使用python(maya)多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆