I’m not sure I understand asynchronous threads correctly. I have the following in the action performed event of a button on a GUI.
def overwrite_database():
try:
#Record event.
backend.messageHandler('HMI', 'info', 'HMI.overwrite_database()')
tbl = event.source.parent.parent.getComponent('tbl_offline')
data = tbl.data
backend.overwrite_database(data)
#--------------------ERROR HANDLING--------------------
except java.lang.Exception:
backend.messageHandler('HMI', 'info', 'Error location: overwrite_database()' + \
'\nError type: ' + str(sys.exc_info()[0]) + \
'\nError message : ' + str(sys.exc_info()[1]))
except Exception:
backend.messageHandler('HMI', 'info', 'Error location: overwrite_database()' + \
'\nError type: ' + str(sys.exc_info()[0]) + \
'\nError message : ' + str(sys.exc_info()[1]))
#--------------------ERROR HANDLING--------------------
system.util.invokeAsynchronous(overwrite_database)'
And then the function backend.overwrite_database(data) is called below from a script:
def overwrite_database(data):
try:
#Record event.
backend.messageHandler('backend', 'info', 'backend.overwrite_database(data[...])')
##########
#do some database related stuff
##########
if i % 100 == 0:
strVal = strVal.rstrip(', ')
sql = strQry + strVal + ';'
args = []
system.db.runPrepUpdate(sql, args, db)
if (lastRow - 100) < 1:
pass
else:
lastRow = lastRow - 100
strQry = "INSERT INTO SCADA.flatFile (line, verName, modelDescrip, menGrp, staNo, seq, workCell, t_Sec, mCode, description, key_Point, quality_Chk, partNo, partName, scan, scan_req, pic, videoFileName) VALUES "
strVal = ""
new_Progress = ((float(max_new) - float(min_new))/(float(max_old) - float(min_old)))*(float(i)-float(max_old))+float(max_new)
future = InductionAutomation.assignLater(window.getRootContainer().getComponent('progressBar'), "value", int(new_Progress))
new_Progress = 100
future = InductionAutomation.assignLater(window.getRootContainer().getComponent('progressBar'), "value", int(new_Progress))
#--------------------ERROR HANDLING--------------------
except java.lang.Exception:
backend.messageHandler('backend', 'info', 'Error location: backend.overwrite_database()' + \
'\nError type: ' + str(sys.exc_info()[0]) + \
'\nError message : ' + str(sys.exc_info()[1]))
except Exception:
backend.messageHandler('backend', 'info', 'Error location: backend.overwrite_database()' + \
'\nError type: ' + str(sys.exc_info()[0]) + \
'\nError message : ' + str(sys.exc_info()[1]))
#--------------------ERROR HANDLING--------------------
And this thread, of which ive edited some parts out, calls a function I believe you created pturmel.
def assignLater(comp, prop, val, ms = 0):
future = CompletableFuture()
def assignLaterInvoked():
try:
try:
setattr(comp, prop, val)
except AttributeError:
comp.setPropertyValue(prop, val)
future.complete(None)
except java.lang.Exception, e:
future.completeExceptionally(e)
except Exception, e:
future.completeExceptionally(PythonAsJavaException(e))
invokeLater(assignLaterInvoked, ms)
return future
So is the last function sending my GUI calls back to the GUI from the asynchronous thread? In my original action performed event I call components on the GUI from an asynchronous thread. Am I leaving myself open for a world of hurt at some point? I’ve heard you mention to never ever do that but I’ve found no problems with the above code. It actually works really well and looks like a proper progress bar updating from 0 to 100.
Any more thoughts are appreciated.