Maya multithreading is it possible without crashes?

Hello everyone! This is my first post here :slight_smile:
I’ll try to explain what I’, trying to do and where I’m stuck. The goal is to write custom log tool for Maya. I mean user have options to enable/disable some sort of log information that is generate by logging module.
So I wrote Maya API plugin. Nothing fancy it just have command which enables om.MCommandMessage.addCommandOutputFilterCallback() and writes log info into text file. This part is working just fine. The problem is in read that file and update QTextEdit() widget line by line. Is it possible do this while Maya is running operations? I mean while it runs some code. The log generated by plugin is working fine while Maya runs other executed code e.g in Script Editor. My intent of using self.repaint() is ended with Maya crash.

So, I know that in ideal world one can run some code in separate thread. In my case it would be read text file and update UI.

Without some trickery, no you can’t touch the GUI or run most maya.cmds.* functions from anything but the main thread.

What you generally need to do is use the utility functions maya.utils.executeDeferred and maya.utils.executeInMainThreadWithResult.

Both of these functions take a callable (think a function) and then put those onto a queue that will then get processed on the main thread during the next idle moment.

http://help.autodesk.com/view/MAYAUL/2018/ENU/?guid=GUID-9B5AECBB-B212-4C92-959A-22599760E91A

Also, if you’re doing anything with modal dialogs, both of those functions become less than reliable, because Maya’s idle queues aren’t all that smart about handling multiple event loops.

Thanks for quick reply. I’ll try this utility functions.

Here’s some info on the GIL - which is good to understand with Python and multithreading:

1 Like

While understanding the GIL is important, it doesn’t hurt python’s ability to use multiple threads, it just blocks parallelism. That is to say, python itself can only execute one operation at a time regardless of the number of threads.

What is causing issues in Maya is that neither the UI framework (Qt), nor Maya API/mel are written in a completely thread safe manner. Instead they have been written in such a way that they expect operations to take place on the main thread, and you can get all kinds of dangerous behavior (usually a memory access violation + crash) by attempting to do such operations on a background thread.

Caveat:
Qt itself has quite a few tools to allow for doing work on other threads, but the core loop of updating the UI itself is not thread safe.

The Maya Parallel Paper is also a good read.

1 Like

Thanks guys. The solution - I have created standalone application. It connects to Maya and sends commands to start log. Then It reads log text file and updates UI using QThread.

FWIW the basic rule of thumb is quite simple:

if you touch the UI or the scene, do it from the main thread.

End of story. You can do all sorts of other work in different threads – but you must use a thread-safe method to initiate any action in the Maya UI or to edit the scene. Sometimes you’ll get lucky from another thread and sometimes you won’t – which makes things worse because you might think things are working when in fact you just happened to hit a lucky window. Either wrap every action in one of the deferred calls or throw your work-to-do into a Queue and poll that from the main thread and act on it during Maya idles.