Jump to content

Execute iron python async (not on the application thread)


Tom Bernens

Recommended Posts

I am in need to execute a iron python script asyncronously, without locking up the main application thread. I have a delegate function already defined but any time I try to spawn a new thread, Spotfire simply crashes. Code snippit below. This code works fine when running a standalone application, I can have a parent form still be active and responsive with my IV form also active and responsive, but in spotfire it doesn't seem to want to behave.

 

def SpawnIV():

iv.ShowDialog()

CleanupAndSave()

iv = ImageViewer(mapping, trellisLevels)

t = Thread(ThreadStart(SpawnIV))

t.SetApartmentState(ApartmentState.STA)

t.Start();

 

 

I have also tried using a worker thread class to invoke the method async, but this simply does nothing:

 

class IVWorker:

def __init__(self, imageViewer):

self.application = Application

self.applicationThread = self.application.GetService[ApplicationThread]()

self.iv = imageViewer

def Start(self):

self.applicationThread.ExecuteOnWorkerThread("IV Worker Thread", self.StartIVWorker)

 

def StartIVWorker(self):

self.applicationThread.InvokeAsynchronously(self.SpawnIV)

def SpawnIV():

self.iv.ShowDialog()

CleanupAndSave()

iv = ImageViewer(mapping, trellisLevels)

ivWorker = IVWorker(iv)

ivWorker.Start()

 

 

EDIT: I have been able to get this (partly) working using a .NET background worker, however due to the multi threaded scope change, I lose referecenses to all of my imports, is there a way to ensure that when spawning the second thread, all of the imports are carried along with it Normally I would use the sys package however this is not installed in Tibco's implementation of IronPython. Inside of the dialog, any actions result in JIT errors like "range" "len" or "zip" is not defined, which are all functions in the __builtin__ namespace, however it seems like every include is unloaded in the second thread....

def bw_DoWork(sender, args):

iv = args.Argument[0]

iv.ShowDialog()

iv = ImageViewer(mapping, trellisLevels)

iv_bw = BackgroundWorker()

iv_bw.DoWork += bw_DoWork

iv_bw.RunWorkerAsync([iv])

 

 

EDIT2: By using the line

import __builtin__

 

at the beginning of every def in my form class, and referencing functions like..

__builtin__.len(), __builtin.range()

 

this problem has been solved

Link to comment
Share on other sites

EDIT: I have been able to get this (partly) working using a .NET background worker, however due to the multi threaded scope change, I lose referecenses to all of my imports, is there a way to ensure that when spawning the second thread, all of the imports are carried along with it Normally I would use the sys package however this is not installed in Tibco's implementation of IronPython. Inside of the dialog, any actions result in JIT errors like "range" "len" or "zip" is not defined, which are all functions in the __builtin__ namespace, however it seems like every include is unloaded in the second thread....

def bw_DoWork(sender, args):

iv = args.Argument[0]

iv.ShowDialog()

iv = ImageViewer(mapping, trellisLevels)

iv_bw = BackgroundWorker()

iv_bw.DoWork += bw_DoWork

iv_bw.RunWorkerAsync([iv])

 

EDIT2: By using the line

import __builtin__

at the beginning of every def in my form class, and referencing functions like..

__builtin__.len(), __builtin.range()

this problem has been solved

Link to comment
Share on other sites

  • 2 years later...
  • 4 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...