允许父应用程序仍然评估的time.sleep? [英] time.sleep that allows parent application to still evaluate?

查看:66
本文介绍了允许父应用程序仍然评估的time.sleep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到最近为Maya和Houdini编写脚本时,我都遇到了一些情况,在这些情况下,我需要等待GUI的各个方面进行更新,然后才能调用其余的Python代码.我当时想在两种情况下都调用time.sleep可以解决我的问题,但似乎time.sleep也可以容纳父应用程序.这意味着无论睡眠是否在那里,我的脚本都会对它们进行完全相同的评估,只是在整个过程中会暂停一下.

I've run into situations as of late when writing scripts for both Maya and Houdini where I need to wait for aspects of the GUI to update before I can call the rest of my Python code. I was thinking calling time.sleep in both situations would have fixed my problem, but it seems that time.sleep just holds up the parent application as well. This means my script evaluates the exact same regardless of whether or not the sleep is in there, it just pauses part way through.

我有想法在Python的单独线程中运行脚本,以查看是否可以释放应用程序以使其在睡眠期间仍然运行,但是我还没有时间对其进行测试.

I have a thought to run my script in a separate thread in Python to see if that will free up the application to still run during the sleep, but I haven't had time to test this yet.

在此期间,我想问一下是否有人知道这种情况的其他解决方案.

Thought I would ask in the meantime if anybody knows of some other solution to this scenario.

推荐答案

Maya-或更准确地说是Maya Python-并不是真正的多线程(Python本身具有一种狡猾的多线程,因为所有线程都为恐惧而战https://stackoverflow.com/questions/265687/why-the-global-interpreter-lock>全局解释器锁定,但这不是您的问题).您可以使用线程模块在Maya中很好地运行线程代码.尝试:

Maya - or more precisely Maya Python - is not really multithreaded (Python itself has a dodgy kind of multithreading because all threads fight for the dread global interpreter lock, but that's not your problem here). You can run threaded code just fine in Maya using the threading module; try:

import time
import threading
def test():
   for n in range (0, 10):
       print "hello"
       time.sleep(1)
 t = threading.Thread(target = test)
 t.start()

这将以一秒钟的间隔向您的听众打印10次"hello",而不会关闭交互性.

That will print 'hello' to your listener 10 times at one second intervals without shutting down interactivity.

不幸的是,maya的许多部分(包括最引人注目的所有用户创建的UI和大多数场景操作)只能从主"线程(拥有maya UI的线程)中运行.因此,您无法使用上述技术来编写脚本来更改窗口中文本框的内容(更糟糕的是,您会收到误导性的错误消息-当您从侦听器运行该代码时该代码有效,但在您从线程调用它,并礼貌地返回完全错误的错误代码).您可以在单独的线程中执行诸如网络通信,写入文件或长时间计算之类的事情-但是,如果您尝试从线程执行UI工作和许多常见的场景任务将失败.

Unfortunately, many parts of maya - including most notably ALL user created UI and most kinds of scene manipulation - can only be run from the "main" thread - the one that owns the maya UI. So, you could not do a script to change the contents of a text box in a window using the technique above (to make it worse, you'll get misleading error messages - code that works when you run it from the listener but errors when you call it from the thread and politely returns completely wrong error codes). You can do things like network communication, writing to a file, or long calculations in a separate thread no problem - but UI work and many common scene tasks will fail if you try to do them from a thread.

Maya在 maya中,有部分解决方法. utils 模块.您可以使用函数executeDeferred和executeInMainThreadWithResult.它们将等待空闲时间运行(例如,这意味着,如果您正在播放动画,它们将不会运行),然后触发,就像在主线程中完成它们一样. Maya文档中的示例给出了这个想法:

Maya has a partial workaround for this in the maya.utils module. You can use the functions executeDeferred and executeInMainThreadWithResult. These will wait for an idle time to run (which means, for example, that they won't run if you're playing back an animation) and then fire as if you'd done them in the main thread. The example from the maya docs give the idea:

import maya.utils import maya.cmds
def doSphere( radius ):
    maya.cmds.sphere( radius=radius )
maya.utils.executeInMainThreadWithResult( doSphere, 5.0 )

这为您提供了大部分所需的东西,但是您需要仔细考虑如何将您的任务分解为线程友好的块.而且,当然,运行线程程序总是比单线程替代方案难,您需要设计代码,以便在工作时如果另一个线程弄乱了变量,事情就不会中断.好的并行编程是一大堆鱼,尽管可以归结为两个基本概念:

This gets you most of what you want but you need to think carefully about how to break up your task into threading-friendly chunks. And, of course, running threaded programs is always harder than the single-threaded alternative, you need to design the code so that things wont break if another thread messes with a variable while you're working. Good parallel programming is a whole big kettle of fish, although boils down to a couple of basic ideas:

1)使用建立对对象的排他控制(用于简短操作) RLocks (在需要时) 2)将共享数据放入安全容器中,例如@dylan示例中的Queue 3)真正清楚哪些对象是可共享的(它们应该是几个!),哪些不是可共享的

1) establish exclusive control over objects (for short operations) using RLocks when needed 2) put shared data into safe containers, like Queue in @dylan's example 3) be really clear about what objects are shareable (they should be few!) and which aren't

这是体面(长)的概述.

对于Houdini,我不确定,但此文章听起来好像在那里也出现了类似的问题.

As for Houdini, i don't know for sure but this article makes it sound like similar issues arise there.

这篇关于允许父应用程序仍然评估的time.sleep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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