Uno commands are getting crashed if document is not opened

Hi ,

I am opening a calc file by below command

        edit_file_path = ":D/untitled.txt"
        office_path = 'soffice'
        calc = '-o'
        pipe = "--accept=socket,host=localhost,port=2002;urp"
        subprocess.Popen([office_path, calc, pipe, edit_file_path])
        time.sleep(10)

and then doing some operations by

    localContext = uno.getComponentContext()
    resolver = localContext.ServiceManager.createInstanceWithContext(
        "com.sun.star.bridge.UnoUrlResolver", localContext)
    initialObject = resolver.resolve(
        "uno:socket,host=localhost,port=2002;urp;"
        "StarOffice.ServiceManager")
    self.ctx = initialObject.getPropertyValue("DefaultContext")
    self.smgr = self.ctx.ServiceManager
    self.desktop = self.smgr.createInstanceWithContext(
        "com.sun.star.frame.Desktop", self.ctx)
    self.frame = self.desktop.getCurrentFrame()
    if not self.frame:
        raise Exception("Could not get frame")
    self.dispatcher = self.smgr.createInstanceWithContext(
        "com.sun.star.frame.DispatchHelper", self.ctx)
    self.transformer = self.smgr.createInstanceWithContext(
        "com.sun.star.util.URLTransformer", self.ctx)
    self.configProvider = self.smgr.createInstanceWithContext(
        "com.sun.star.configuration.ConfigurationProvider", self.ctx)

However sometimes if file is not open in less time , (may be because of taking some processing time, or sometime document ask user to select options like discard the previous one) , then the next uno commands fail with exception.
Is there any way I can check whether file is opened and connection is made before executing the other uno commands.
Sleep is not preferable as user can take infinite time if document wants user to provide some inputs.

Thanks a lot

What is the exception, and on what line does it occur?

By the way, where I come from, “thanks a lot” by itself (usage 2 in this entry) means that someone is not being helpful. Instead, you could say “Thanks very much.”

ohh my bad, Thanks very much.
Actually error was coming on below line , when libreoffice is asking for previous document recovery and want user to confirm. In that case, if user doesn’t confirm very fast, frame will come as None.

 self.frame = self.desktop.getCurrentFrame()
    if not self.frame:
        raise Exception("Could not get frame")

I have done a workaround of sleeping and trying again if frame is none. Now it is working good for the document recovery case. However i think It may hang the program if frame is None for some other reason.

Catch the exception and keep trying until the command is successful. Often in programming, it’s better to ask for forgiveness than permission.

I tested the following code by removing time.sleep(10) from your example. This may not be the exception you asked about, but perhaps the same approach could be used.

from com.sun.star.connection import NoConnectException

initialObject = None
while not initialObject:
    try:
        initialObject = resolver.resolve(
            "uno:socket,host=localhost,port=2002;urp;"
            "StarOffice.ServiceManager")
    except NoConnectException:
        time.sleep(1)