I am working with Python UNO using the "internal" (Libreoffice is the host process) version where the Python interpreter lives within Libreoffice/Openoffice. I would like to make the code nonblocking...that is once the code gets called as a macro, it starts a second thread and returns the main thread back to Office so that it does not block the UI while it continues to run a very lengthy process (10-20 minutes execution time).
When I tried exactly this, LibreOffice freezes forever. I've searched just about everywhere but other than an odd reference to importing scipy in a second thread and then blocking while waiting on that thread (myrhread.join()), there seems to be nowhere that this is done.
Alternatively, is it possible to create a new ServiceManager so that I could invoke a second process and then link back to the ServiceManager so that I can return back to LibreOffice in the normal way without locking it up with a "ghost thread"?
After a lot of digging, I found the answer here:
LibreOfficeForum Threading Example
For simple long-running tasks where it would otherwise block the UI thread, this works very well. The relevant code is as follows:
from threading import Thread from time import sleep import uno t = None def test_worker(doc): # Wait 30 seconds for demonstration purposes sleep(30) # Get the 1st sheet in the document and insert text into cell A1 doc.Sheets.getByIndex(0).getCellByPosition(0,0).String = "I'm back" def delayedRun(*args): global t doc = XSCRIPTCONTEXT.getDocument() t = Thread(target = test_worker, args = (doc,)) t.start() g_exportedScript = delayedRun,