Python UNO 和线程
Python UNO and Threads
我正在使用 Python UNO 使用 "internal"(Libreoffice 是主机进程)版本,其中 Python 解释器位于 Libreoffice/Openoffice 中。我想让代码成为非阻塞的……一旦代码被作为宏调用,它就会启动第二个线程,然后 returns 将主线程返回到 Office,这样它就不会阻塞 UI 同时它继续 运行 一个非常漫长的过程(执行时间为 10-20 分钟)。
当我完全尝试这个时,LibreOffice 永远冻结。我几乎到处搜索,除了在第二个线程中导入 scipy 然后在等待该线程 (myrhread.join()) 时阻塞的奇怪引用之外,似乎没有任何地方是完成。
或者,是否可以创建一个新的 ServiceManager,以便我可以调用第二个进程,然后 link 回到 ServiceManager,这样我就可以 return 以正常方式回到 LibreOffice没有用 "ghost thread"?
锁定它
经过大量挖掘,我在这里找到了答案:
LibreOfficeForum Threading Example
对于简单的 long-运行 任务,否则它会阻塞 UI 线程,这非常有效。相关代码如下:
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,
我正在使用 Python UNO 使用 "internal"(Libreoffice 是主机进程)版本,其中 Python 解释器位于 Libreoffice/Openoffice 中。我想让代码成为非阻塞的……一旦代码被作为宏调用,它就会启动第二个线程,然后 returns 将主线程返回到 Office,这样它就不会阻塞 UI 同时它继续 运行 一个非常漫长的过程(执行时间为 10-20 分钟)。
当我完全尝试这个时,LibreOffice 永远冻结。我几乎到处搜索,除了在第二个线程中导入 scipy 然后在等待该线程 (myrhread.join()) 时阻塞的奇怪引用之外,似乎没有任何地方是完成。
或者,是否可以创建一个新的 ServiceManager,以便我可以调用第二个进程,然后 link 回到 ServiceManager,这样我就可以 return 以正常方式回到 LibreOffice没有用 "ghost thread"?
锁定它经过大量挖掘,我在这里找到了答案:
LibreOfficeForum Threading Example
对于简单的 long-运行 任务,否则它会阻塞 UI 线程,这非常有效。相关代码如下:
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,