Hello everyone!

I've been having some issues lately with Maya and QThread that I hope 
someone can help me, because I surely don't have any idea why that would be.

The crash I'm having is another variant of what Viktor Petrov asked here 
<https://stackoverflow.com/questions/56629553/proper-pyside-qthread-use-in-maya-to-avoid-hard-crash>,
 
a sudden crash with no dialog or warning, just Maya closing unexpectedly. 
Inside of Maya I'm using a QThread that queries some data through HTTP 
requests (in my case specifically, this is done by the Ftrack 
<https://www.ftrack.com/en/> API) and emits the result as a string through 
a signal. This tool works on Nuke as well and so far our Nuke users have 
not reported any issues of this kind.

The crashes seem random but constant. There are no traceback or errors. 
I've gotten a process dump on the moment of the crash but to be honest 
either I don't know how to interpret it or it really doesn't help.

I've also tried to switch to QRunnable + QThreadPool but doesn't solve the 
issue either. All data is interchanged as a string and I'm using a queue.Queue 
instance which should be thread safe. There is a small delay between the 
thread starting and the crash which leads me to think that the reason is 
either the result signal or the HTTP request itself.

Is there any special precautions to use when dealing with QThread + HTTP 
requests in Maya?

This is a slightly modified version of the code I'm using:

from six.moves import queue as thqueueimport jsonfrom Qt import QtCore
class DataRetrievalThread(QtCore.QObject):
    failed = QtCore.Signal(object)
    dataReady = QtCore.Signal(object)

    def __init__(self, job_queue=None, parent=None):
        super(DataRetrievalThread, self).__init__(parent=None)
        self.job_queue = job_queue or thqueue.Queue()

    def run(self):
        while not self.job_queue.empty():
            try:
                self.run_next_job()
            except Exception as e:
                self.failed.emit(str(e))
            finally:
                self.job_queue.task_done()

    def run_next_job(self):
        next_ = json.loads(self.job_queue.get())

        action = next_['action']
        data = next_['data']
        response = next_.copy()

        if action == 'action1':
            self.do_action1(data=data, response=response)

        if action == 'action2':
            self.do_action2(data=data, response=response)

        if action == 'action3':
            self.do_action3(data=data, response=response)

        if action == 'action3':
            self.do_action3(data=data, response=response)

        else:
            raise NotImplementedError('Action "{}" not implemented'
                                      .format(action))

        self.dataReady.emit(json.dumps(response))

​

This is the code called from the main process, requestData is called to 
start a job and onThreadJobFinished is the callback when the thread 
finishes. The events have a unique ID to make sure no python object is sent 
to the thread, but that didn't solve the issue either.


def requestData(self, action, data, callback=None):
    event = {'id': str(uuid.uuid4()),
             'data': data,
             'action': action,
             'callback': callback,
             'source': self._name}
    self.events[event['id']] = event

    serialized_event = event.copy()
    serialized_event.pop('callback')
    self.thread_queue.put(json.dumps(serialized_event))

    if not self.thread.isRunning() and self.thread_ready:
        self.thread.start()

    return event['id']
def onThreadJobFinished(self, event):
    if event['source'] != self._name:
        return

    orig_event = self.events[event['id']]

    if orig_event['callback']:
        orig_event['callback'](event)

    self.events[event['id']]['result'] = event['result']

​


Thanks,
Salvador.

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/c0607bf4-0735-46c8-ba1b-f14cb91e4580%40googlegroups.com.

Reply via email to