在Maya中正确使用PySide QThread以避免硬崩溃 [英] Proper PySide QThread use in Maya to avoid hard crash

查看:402
本文介绍了在Maya中正确使用PySide QThread以避免硬崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用QThreads在Maya中更新自定义工具基于Qt的UI.我有一个线程,该线程可以执行任意方法,并通过发出的信号返回结果,然后使用该信号来更新我的UI.这是我自定义的QThread类:

I'm attempting to use QThreads to update my custom tool's Qt-based UI inside of Maya. I have a thread that executes arbitrary methods and returns the result via an emitted signal, which I then use to update my UI. Here's my custom QThread class:

from PySide import QtCore


class Thread(QtCore.QThread):

    result = QtCore.Signal(object)

    def __init__(self, parent, method, **kwargs):
        super(Thread, self).__init__(parent)
        self.parent = parent
        self.method = method
        self.kwargs = kwargs

    def run(self):
        result = self.method(**self.kwargs)
        self.result.emit(result)

我传递给线程的方法是从网址获取序列化数据的基本请求,例如:

The methods I'm passing to the thread are basic requests for getting serialized data from a web address, for example:

import requests

def request_method(address):
    request = requests.get(address)
    return request.json()

这是我在自定义工具中使用线程动态更新UI的方式:

And here is how I use the thread in my custom tool to dynamically update my UI:

...
    thread = Thread(parent=self, method=request_method, address='http://www.example.com/')
    thread.result.connect(self._slot_result)
    thread.start()

def _slot_result(self, result):
    # Use the resulting data to update some UI element:
    self.label.setText(result)
...

此工作流程可在其他DCC(例如Nuke)中使用,但由于某些原因,它有时会导致Maya崩溃并不一致.没有错误信息,没有日志,只是严重的崩溃.

This workflow works in other DCCs like Nuke, but for some reason it causes Maya to sometimes crash inconsistently. No error message, no log, just a hard crash.

这使我认为我的QThread工作流程设计显然不适合Maya.有什么想法可以最好地避免在使用QThreads时使Maya崩溃,以及什么可能导致此特定问题?

This makes me think that my QThread workflow design is obviously not Maya-friendly. Any ideas how best to avoid crashing Maya when using QThreads and what may be causing this particular issue?

推荐答案

我们工作室的一位工程师发现了一些与使用Python线程和PyQt/PySide有关的错误.请参考:

One of the engineers at our studio discovered a few bugs related to the use of Python threads and PyQt/PySide. Please refer to:

  • [PySide 1.x] https://bugreports.qt.io/browse/PYSIDE-810
  • [PySide 2.x] https://bugreports.qt.io/browse/PYSIDE-813

记者的笔记:

尽管QObject是可重入的,但GUI类(尤其是QWidget及其所有子类)不是可重入的.只能在主线程中使用它们.

Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.

这篇关于在Maya中正确使用PySide QThread以避免硬崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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