How to run multiple instances of Writer simultaneously?

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


On 19 October 2017 at 15:05, Jens Tröger <jens.troeger at light-speed.de> 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.

Regards
Matthew Francis


#!/usr/bin/python3

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

from com.sun.star.connection 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
else:
    os.mkdir(userProfile)
    profileCached = False

# Launch LibreOffice server
pipeName = uuid.uuid4().hex
args = [
    sofficePath,
    '-env:UserInstallation=file://' + userProfile,
    '--pidfile=' + tempDir + '/soffice.pid',
    '--accept=pipe,name=' + pipeName + ';urp;',
    '--norestore',
    '--invisible'
]
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):
    try:
        localContext = uno.getComponentContext()
        resolver =
localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",
localContext)
        context =
resolver.resolve("uno:pipe,name=%s;urp;StarOffice.ComponentContext" %
pipeName)
        break
    except NoConnectException:
        time.sleep(0.1)
        if i == 99:
            raise

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


desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop",
context)
desktop.loadComponentFromURL('private:factory/swriter', 'blank', 0, ())

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


More information about the LibreOffice mailing list