Um? Why does this 'soffice' invocation return immediately? Well, why is this invocation so slow?
Topics
About: UNO (Universal Network Objects)
About: LibreOffice
About: Apache OpenOffice
The table of contents of this article
- Starting Context
- Target Context
- Orientation
- Main Body
- 1: The Distinction Between 'soffice' or 'soffice.exe' and 'soffice.bin'
- 2: Let Us Define 'LibreOffice Instance' or 'Apache OpenOffice Instance' (Also Known as 'Office Instance')
- 3: 'Office Instance' Is a Singleton per Operating System User
- 4: Let Us Demonstrate How 'soffice' (for Linux) or 'soffice.exe' (for Windows) Works
- 5: The Conclusion and Beyond
Starting Context
- The reader has knowledge on what UNO is and how it is related to LibreOffice or Apache OpenOffice.
- The reader has knowledge on how to make any LibreOffice or Apache OpenOffice instance a UNO server.
Target Context
- The reader will understand how any 'soffice' or 'soffice.exe' execution works.
Here are Hypothesizer 7 and Objector 13A in front of a Windows computer screen.
Orientation
Objector 13A
Huh? Why? This doesn't make sense!
Hypothesizer 7
. . . Sir, what does not make sense, if I may ask?
Objector 13A
My C# program invokes 'soffice.exe' and waits until the LibreOffice process terminates.
Hypothesizer 7
Hmm . . .
Objector 13A
But the invocation returns immediately without waiting for the LibreOffice process to terminate!
Hypothesizer 7
Ah . . .
Objector 13A
"Ah . . ." what?
Hypothesizer 7
You are confusing the 'soffice.exe' invocation's returning with the LibreOffice instance's terminating, are not you?
Objector 13A
Huh? What do you mean by "confusing"?
Hypothesizer 7
Well, in other words, you are confusing the 'soffice.bin' process with the LibreOffice instance, are not you?
Objector 13A
Huh? What is "soffice.bin"? You mean 'soffice.exe', don't you?
Hypothesizer 7
No. I mean "soffice.bin". . . . Sir, what does your program want to accomplish by invoking 'soffice.exe', if I may ask?
Objector 13A
My program wants to open a document file on the desktop by LibreOffice, wait until the user finishes modifying the document, and then process the document file. My program definitely has to wait until the user finishes modifying the document.
Hypothesizer 7
Ah . . .
Objector 13A
"Ah . . ." what?
Hypothesizer 7
Considering your situation, I am certain that a LibreOffice instance had been up when your program invoked 'soffice.exe', and the 'soffice.bin' process worked as a UNO client to the existing LibreOffice instance, without becoming any full-fledged LibreOffice process.
Objector 13A
Huh? Are you talking in your sleep? "UNO"? "UNO client"?
Hypothesizer 7
Well, maybe, you will have to understand what UNO is and how it is related to LibreOffice or Apache OpenOffice, in order to understand what is going on.
Objector 13A
. . .
Main Body
1: The Distinction Between 'soffice' or 'soffice.exe' and 'soffice.bin'
Hypothesizer 7
As a preliminary knowledge, let us understand the distinction between 'soffice' (for Linux) or 'soffice.exe' (for Windows) and 'soffice.bin'.
'soffice.bin' is the main program of LibreOffice or Apache OpenOffice, and 'soffice' or 'soffice.exe' is an invoker of a 'soffice.bin' process.
In Linux, 'soffice' is a shell script, which is the reason why the 'ps' command shows a 'soffice.bin' process, but not any 'soffice' process (any shell script itself does not become any process, but is instructions data read by a shell process), when 'soffice' is executed.
In Windows, 'Task Manager' shows a 'soffice.bin' process and a 'soffice.exe' process (as 'soffice.exe' is not any batch file, but a proper executable file) when 'soffice.exe' is executed.
2: Let Us Define 'LibreOffice Instance' or 'Apache OpenOffice Instance' (Also Known as 'Office Instance')
Hypothesizer 7
Logically speaking, anyone can arbitrarily define any term, but at least, we have to distinguish any two different concepts by giving them different names.
So, we will call any full-fledged LibreOffice process or Apache OpenOffice process 'LibreOffice instance' or 'Apache OpenOffice instance' (I will also use 'office instance' because always citing the 2 names would be bothersome for some readers), while we will call any 'soffice.bin' process ''soffice.bin' process'. . . . In fact, using the terms, 'LibreOffice process' or 'Apache OpenOffice process' or 'office process', is fine, but I will usually use 'instance'.
Objector 13A
. . . What do you mean by "full-fledged LibreOffice process or Apache OpenOffice process"?
Hypothesizer 7
Any full-fledged LibreOffice process or Apache OpenOffice process is a process that has the ability to really execute the main functionality of LibreOffice or Apache OpenOffice.
Objector 13A
. . . What do you mean by "really execute"?
Hypothesizer 7
I mean that the process executes the main functionality of LibreOffice or Apache OpenOffice in itself, not ask another process to execute the functionality.
Objector 13A
Hmm . . ., still I don't understand how a 'soffice.bin' process isn't an office instance.
Hypothesizer 7
That will become clear in the next section.
3: 'Office Instance' Is a Singleton per Operating System User
Hypothesizer 7
'office instance' is a singleton per operating system user, which means that there cannot be more than one office instance per operating system user at any time.
Objector 13A
. . . which means?
Hypothesizer 7
When an operating system user has no office instance, any 'soffice' or 'soffice.exe' execution by the user starts up an office instance.
Objector 13A
For example, when my program executes 'soffice.exe' with a document file path as the argument, . . .
Hypothesizer 7
That starts up an office instance, which opens the document file in itself.
Objector 13A
That's understandable.
Hypothesizer 7
On the other hand, when the user already has an office instance (there cannot be more than one), any 'soffice' or 'soffice.exe' execution does not start up any new office instance, but becomes a UNO client that connects to the existing office instance.
Objector 13A
But I have not set up my LibreOffice as any UNO server as your article says.
Hypothesizer 7
Ah, a good point. As there is a description in the article, the office instance can be turned into a UNO server anytime by the 'soffice.exe' command. In fact, the second 'soffice.exe' command exactly did so.
Objector 13A
Has it turned the office instance into a socket UNO server?
Hypothesizer 7
No, it has turned the office instance into a named pipe UNO server.
Objector 13A
Well, for example, when my program executes 'soffice.exe' with a document file path as the argument in that situation, . . .
Hypothesizer 7
The 'soffice.bin' process does not open the document file in itself, but asks the existing office instance to open the document file, and ends.
Objector 13A
. . . What happens to the document?
Hypothesizer 7
The document is kept opened in the office instance, until it is explicitly closed.
Objector 13A
So, the 'soffice.exe' invocation returns immediately without waiting for the document to be closed, of course.
Hypothesizer 7
. . . Of course.
Note that another operating system user can have another office instance, and any 'soffice.bin' process does not connect to the office instance of another operating system user, which is what I mean by "'office instance' is a singleton per operating system user".
4: Let Us Demonstrate How 'soffice' (for Linux) or 'soffice.exe' (for Windows) Works
Hypothesizer 7
Let us demonstrate that any 'soffice.exe' invocation behaves as we have learned in the last section.
Please open a terminal and execute the 'soffice.exe' command (the 'soffice.exe' file exists in the 'program' directory of the LibreOffice directory) with the '--writer' option (which means to open a new Writer document), after you have confirmed that there is no office instance up.
Stage Direction
Objector 13A shuts down a previous LibreOffice instance, opens a terminal, and executes this on the terminal.
@cmd Source Code
soffice.exe --writer
Objector 13A
It has returned immediately . . .
Hypothesizer 7
You should do 'start /wait soffice.exe --writer': the returning immediately is not about 'soffice.exe', but about the terminal.
Stage Direction
Objector 13A shuts down the previous LibreOffice instance, and executes this on the terminal.
@cmd Source Code
start /wait soffice.exe --writer
Objector 13A
Ah, the new document opens in a new window on the desktop, and the command doesn't return immediately.
Hypothesizer 7
The command has started an office instance, which has opened the new Writer document in itself.
Please execute the same command once more with the office instance kept up.
Stage Direction
Objector 13A opens another terminal, and executes this on the terminal.
@cmd Source Code
start /wait soffice.exe --writer
Objector 13A
The new document opens in a new window on the desktop, but the command returns immediately, while the newly opened window keeps open . . .
Hypothesizer 7
The second command connected to the existing office instance, asked the office instance to open a new Writer document, and has ended; the new window belongs to the existing office instance.
5: The Conclusion and Beyond
Hypothesizer 7
Now, we understand how any 'soffice' or 'soffice.exe' execution works.
Invoking 'soffice' or 'soffice.exe' does not necessarily mean starting up a new LibreOffice or Apache OpenOffice instance (office instance), and the invocation's returning does not necessarily mean the termination of any office instance.
If a program has to exert control like waiting for the termination of an office instance (and the closure of a document that is opened in the office instance) by waiting for a 'soffice' or 'soffice.exe' invocation's returning, the program will have to ascertain that any office instance is not up for the operating system user before the invocation.
However, as the user can usually freely start up any office instance, such enforcement is usually difficult, and starting up and shutting down an office instance for each action will be very inefficient. Instead, why do not we use an office instance as a UNO server, and create our own UNO client that handles the UNO server?
Creating our own UNO client is not so tedious as someone may imagine, and I have published some UNO console client samples (in Java, in C++, in C#, and in Python) and a UNO GUI client sample (in Java; the UNO GUI client samples of the other programming languages will come later).
While there are some people who call 'soffice --convert-to' commands from their programs, those commands work exactly in the way explained in this article and may not be so desirable efficiency-wise and concurrency-wise, in addition to functionality-wise. There is an article on optimally using LibreOffice or Apache OpenOffice as a files converter (the concept, a Java implementation, a C++ implementation, a C# implementation, a Python implementation, and a LibreOffice Basic implementation).