How to run multiple instances of Writer simultaneously?

Matthew Francis mjay.francis at
Thu Oct 19 08:26:47 UTC 2017

On 19 October 2017 at 15:05, Jens Tröger <jens.troeger at> wrote:
> The termination of the process p seems to destroy the other running soffice process. Hence using custom user installations with -env:UserInstallation=/tmp/random-dir but that just prolongs the process creation noticeably!

One alternative to allowing LO to create a new user installation each
time (which will indeed be slow) is to create a fresh user
installation once, save it somewhere as a template, then make a new
copy of the template directory each time - a file level copy should be
considerably faster.
You can accomplish this simply in Python using shutil.copytree()

Some sample code appended below - this is hacked out from a previous
project, and may or may not represent best practice as to how to do
this, but does work.

Matthew Francis


import os
import re
import shutil
import subprocess
import tempfile
import time
import uno
import uuid

from import NoConnectException

sofficePath = '/usr/bin/soffice'
tempDir = tempfile.mkdtemp()

# Restore cached profile if available
userProfile = tempDir + '/profile'
cacheDir = os.getenv('XDG_CACHE_DIR', os.environ['HOME'] + '/.cache')
+ '/lo_profile_cache'
if os.path.isdir(cacheDir):
    shutil.copytree(cacheDir, userProfile)
    profileCached = True
    profileCached = False

# Launch LibreOffice server
pipeName = uuid.uuid4().hex
args = [
    '-env:UserInstallation=file://' + userProfile,
    '--pidfile=' + tempDir + '/',
    '--accept=pipe,name=' + pipeName + ';urp;',
sofficeEnvironment = os.environ
sofficeEnvironment['TMPDIR'] = tempDir
proc = subprocess.Popen(args, env=sofficeEnvironment, preexec_fn=os.setsid)

# Open connection to server
for i in range(100):
        localContext = uno.getComponentContext()
        resolver =
        context =
resolver.resolve("uno:pipe,name=%s;urp;StarOffice.ComponentContext" %
    except NoConnectException:
        if i == 99:

# Cache profile if required
if not profileCached:
    shutil.copytree(userProfile, cacheDir)

desktop = context.ServiceManager.createInstanceWithContext("",
desktop.loadComponentFromURL('private:factory/swriter', 'blank', 0, ())

# ... clean up temporary directory when done (omitted)

More information about the LibreOffice mailing list