different LibO program execution behaviour between wait and time.sleep command

A questioneer takes measurement data from a serialport. The aim is to insert the value in consequetive calc sheet rows, together with a timestamp. Upon elaborating this further I used the code below, the observed behaviour is

  • testSleep results in an idle LibO interface, showing all time values after five seconds, when for-loop execution is finished. Not very desirable in case you want to observe a logging of actual data
  • testWait results in an active LibO interface, updating the next row every second.

    This might also play a role in this post mouse events blocked and this listener closes ‘endless’ loop

    I also observed this behaviour when moving the rowpointer in a one-to-many SubForm_Grid. A sleep command was not succesful to spare the interface some time to keep up with the program execution, the invoke basic wait did the trick.

    The idleness of the interface is a bit counter intuitive. Is there an explanation, can it be made intuitive without this workaround?
    LinuxMint 20 LO 6.4.7.2
 
import datetime
import time

def testSleep():
    sheet = XSCRIPTCONTEXT.getDocument().getSheets().getByIndex(0)

    for i in range(5):
        dat = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
        cell = sheet.getCellByPosition(2,i)
        time.sleep(1)
        cell.setString(dat)

def testWait():
    sheet = XSCRIPTCONTEXT.getDocument().getSheets().getByIndex(0)
    oDoc = getBasicScript("waitPython")

    for i in range(5):
        dat = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
        cell = sheet.getCellByPosition(2,i)  # = C1
        oDoc.invoke((), (), ())
        cell.setString(dat)

def getBasicScript(macro='Main', module='Module1', library='Standard',
        isEmbedded=False) ->XScript:

    if isEmbedded:
        desktop = smgr.createInstanceWithContext('com.sun.star.frame.Desktop', ctx)
        scriptPro = desktop.CurrentComponent.getScriptProvider()
        location = "document"
    else:
        mspf = smgr.createInstanceWithContext(
            "com.sun.star.script.provider.MasterScriptProviderFactory", ctx)
        scriptPro = mspf.createScriptProvider("")
        location = "application"
    scriptName = "vnd.sun.star.script:"+library+"."+module+"."+macro+ \
                 "?language=Basic&location="+location
    xScript = scriptPro.getScript(scriptName)
    return xScript
# the BASIC routine is a simple
# sub waitPython()
#     wait(1000)
#End Sub 

To keep the GUI thread running while executing a Python macro, use threading, for example https://stackoverflow.com/a/35853062/5100564 or Python macro - Calc - Stuck with getCellByPosition and threads.

Calling a Basic routine also works, as you have done, so if you find that way easier then I’d say there’s nothing wrong with that. Performance may not be as good with that approach though.

Personally I prefer using Python threads, as that gives more control about what exactly is going on. This also relates to what counts as intuitive - to me, the fact that Python blocks the GUI thread makes more sense than how it does not in Basic. But I can understand how the reverse could be said.

the fact that Python blocks the GUI thread makes more sense than how it does not in Basic

… but indeed it can’t be made differently in Basic, since there you have no option to choose threads. :slight_smile: