pythonmultiprocessingpicklelibreoffice-calclibreoffice-macros

Avoid PicklingError using multiprocessing in Python 3.8 libreoffice macro


I'm trying to use multiprocessing in a LibreOffice Macro in Python 3.8. When I create a Macro it is part of the 'ooo_script_framework' module. So, when the process is pickled it is in fact part of an object which is not pickelable.

"Can't pickle \<function task at 0x0000017B53415AF0\>: import of module 'ooo_script_framework' failed"

I'd like to be able to make the function declared in the main process instead without any link to the module.

MyMacro.py :

import multiprocessing as mp
import time

def tack():
   with open("./test.txt", "w") as f:
      f.write("toto")

def MyMacro():
   p = mp.Process(task)
   p.start()
   time.sleep(1)
   p.stop()

It seems that MyMacro.py is somehow copied into something like ooo_script_framework.py.

ooo_script_framework.py :

insert MyMacro.py

How to make the interpreter not able to see ooo_script_framework.task(), but just tack()?


Solution

  • Use threading.Thread instead, which works well in macros. For example, https://stackoverflow.com/a/35853062/5100564.

    The multiprocessing module should be run under if __name__ == "__main__":, at least on Windows, but that isn't how macros are executed.