IronPython threading in ACT

Options

I try to use multithreading in ACT ironPython. The code is as follow:

# -*- coding: utf-8 -*-
import threading
import time
class PythonThreads():
    def __init__(self):
        self.threads = []

    def Add(self, func, args = None):
        t = self.Thread(func,args)
        self.threads.append(t)

    def Run(self):
        for t in self.threads:
            t.start()

    def Join(self, Count = None, Label = None):
        for t in self.threads:
            t.join()

        for t in self.threads:
            if not t.is_alive() and Count != None:
                Count[0] += 1

    def GetResults(self):
        Results = [None] * len(self.threads)
        for index, t in enumerate(self.threads):
            Results[index] = t.getResult()
        return Results

    class Thread(threading.Thread):
        def __init__(self, func, args = (), name=''):
            threading.Thread.__init__(self)
            self.name = name
            self.func = func
            self.args = args

        def run(self):
            if self.args == None:
                self.args = ()
            self.result = self.func(*self.args)

        def getResult(self):
            return self.result

tt = PythonThreads()
def test():
    time.sleep(5)
    return 1

thread1 = PythonThreads()
t1 = time.time()

for i in range(3):
    thread1.Add(test)
thread1.Run()
thread1.Join()
result = thread1.GetResults()
t2 = time.time()
print t2-t1
print result
# Results = tt.GetResults()
# ExtAPI.Log.WriteMessage(str(Results))

This can work using "C:\Program Files\ANSYS Inc\v231\commonfiles\IronPython\ipy.exe" testThread.py in CMD command shell.
But in ACT, it is occur error.

Comments

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 279
    First Anniversary First Comment 5 Likes Ansys Employee
    Options

    What is the t.getResult doing? Seems like you are doing multiple post processing at same time?

  • 1990chs
    1990chs Member Posts: 43
    First Comment Name Dropper
    edited February 20
    Options

    @Mike.Thompson said:
    What is the t.getResult doing? Seems like you are doing multiple post processing at same time?

    t.getResult is to get the function return result. This is from "Core Python Applications programming (third edition)" book.
    Yes, I try to use multi threads to extract the post result to reduce the time. But the IronPython version in workbench is 2.7.4, and it don't have multiprocessing module, so I only have to use threading module.