Hi List,
i tried to optimize performance of a scripted operator i wrote, by
adding multithread-support.
So i got a Test-Script working fine in Sublime-Text, but as soon as i
try to run is from inside Softimage's ScriptEditor or in a
ScriptedOperator it breaks.
It gives me this error:
# PicklingError: Can't pickle <class '__ax_main__.Worker'>: it's not
found as __ax_main__.Worker
Cant find any results in Google, but maybe someone here know how to do it
PS:
No, in this case i cant go to ICE and do my stuff there. ;-)
import multiprocessing
class Worker(multiprocessing.Process):
def __init__(self, num, outQue):
multiprocessing.Process.__init__(self)
nThreads = multiprocessing.cpu_count()
self.num = num
self.low = num * (tMax/nThreads)
self.high = (num+1) * (tMax/nThreads)
self.outQue = outQue
def run(self):
out = 0
for i in range(self.low, self.high):
out+=i
print "Thread:" + str(self.num) + " (" + str(self.low) + " - "
+ str(self.high) + ")" + " -> " + str(out) #Debug
self.outQue.put(out)
##############################
# Main
##############################
#if __name__ == "__main__": # Sublime
if __name__ == "__ax_main__": # XSI
nThreads = multiprocessing.cpu_count()
results = multiprocessing.Queue() # Queue to Store Results global
tMax = 123456789
# Create and Start workers
workers = []
for i in range(nThreads):
w = Worker(i, results)
workers.append(w)
w.start()
# Wait for Workers to Finish and get Results from Queue
out = 0
for w in workers:
w.join()
out+=results.get()
print "------"
print "Result: " + '{:,}'.format(out)
print "------"