multithreading - Maya threading causing crash -


i've started autosave script editor script (using maya 2014), it's unstable , can crash if happens @ same time save. realised crashes happen when not saving, tried find actual problem was, , ended barely code left still able replicate it.

my idea code run background thread, it'll loop , backup scripts @ interval, check value every second make sure it's not been paused or cancelled (cancelled stop loop).

i presume problem how background threads work in maya, can crash if loading/closing script editor window, or switching tabs on render view settings (at least mental ray selected, since seems take longer loading tabs default renderer). presume there's other ways, ones easy find.

after getting down time.sleep() in while loop, doesn't make sense me why should causing crash. used different sleep function while time.time()>starttime+1, make sure wasn't time module, still caused crashes.

here cut down code if wants try it, once start thread autosave.start(), if continuously load , close script editor window, should runtime error (that says r6025 pure virtual function call). may take multiple attempts, seems happen.

import threading, time import pymel.core pm  class autosavethread(object):     def __init__( self ):         thread = threading.thread(target=self.run, args=())         thread.daemon = true         thread.start()     def run(self):         while true:             time.sleep(1)             print "open , close script editor enough times , crash"  class autosave:     @classmethod     def start( self ):         autosavethread() 

i have dozen or tabs open loading/closing takes bit longer if had none, potentially increase time window in crashes can happen.

for record, here bit of code built maya run whenever script editor window closed. thought may have modified version of saving, trying save @ same time, it's still crashing nothing happening in loop.

global proc syncexecuterbackupfiles(){     global string $gcommandexecuter[];     global string $executerbackupfilename;      if(`optionvar -q saveactionsscripteditor`) {         // clear script editor temp dir first before writing temp files         string $scripteditortempdir = (`internalvar -userprefdir` + "scripteditortemp/");         string $tempfiles[] = `getfilelist -folder $scripteditortempdir`;         string $file;         ($file in $tempfiles) {             sysfile -delete ($scripteditortempdir + $file);         }          // save executer control text files         int $i = 0;         for($i = 0; $i < size($gcommandexecuter); $i++) {             cmdscrollfieldexecuter -e -storecontents $executerbackupfilename $gcommandexecuter[$i];         }     } } 

try wrapping call print in pymel.mayautils.executedeferred or maya.utils.executedeferred executed on main ui thread.


if continuously load , close script editor window, should runtime error (that says r6025 pure virtual function call). may take multiple attempts, seems happen.

i able confirm behavior on maya 2012, , doubt it's version specific.

my bet test call print causing maya crash, because though print python statement, maya has sort of hook update script editor's output window (and potentially command response bar) string printing, both running on main ui thread.

from autodesk knowledge article "python , threading":

maya api , maya command architectures not thread-safe. maya commands throw exception if called outside main thread, , use of openmaya api threads other main 1 has unforeseen side effects.

by passing print statement pymel.mayautils.executedeferred i've (at least far, knows maya ;-) ) been unable cause crash.

import threading, time import pymel.core pm  import pymel.mayautils  # maya.utils, executedeferred  # set false @ time allow threads stop keep_threads_alive = true  def wrapped_print():     print "opening , closing script editor shouldn't make crash\n"  class autosavethread(object):     def __init__(self):         thread = threading.thread(target=self.run)         thread.start()     def run(self):         while keep_threads_alive:             time.sleep(1)             pymel.mayautils.executedeferred(wrapped_print)  ... 

the side-effect of wrapping print statement no longer echoes command response bar. if preserving behavior important use pymel.mel.mprint instead.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -