Parallel processing(QThreadPool, multiprocessing...) in Krita Python scripts

My environment is Krita 5.3.0 (prealpha) / Windows 11 (x64).

I’m developing a Krita plugin with Python.

Currently, I’m implementing multithreading for this script, but due to Python(GIL) constraints, I have not seen any noticeable effects from parallel processing using QThreadPool.

Therefore, I tried using the multiprocessing module from Python’s standard library for parallel processing.

import multiprocessing as mp

class TestFilterProc():
	def __init__(self, param, cancel_event, event_queue):
		self.param = param
		self.process = mp.Process(target=self.run)
		self.cancel_event = cancel_event
		self.event_queue = event_queue

	def run(self):
		pass # Heavy process

class TestFilter(QObject):
	progress = pyqtSignal(int, int)
	completed = pyqtSignal()
	finished = pyqtSignal()

	def __init__(self, param):
		super().__init__()
		self.mp_manager = mp.Manager()
		self.mp_queue = mp.Queue()
		self.cancel_event = self.mp_manager.Event()

		self.myproc = TestFilterProc(param, self.cancel_event, self.mp_queue)

	def applyFilter(self):
		self.myproc.start()

But when attempting to execute functions such as “multiprocessing.Manager()”, “multiprocessing.Queue()”, or “multiprocessing.Process()/start()” required for communication with the main thread, a warning stating “krita: unknown option: c, multiprocessing-fork.” appears, causing the entire application to freeze.

Note that it works fine when executed directly in the Python interpreter without using Krita.

Please let me know if there is a solution to this problem, or if there are any effective parallel processing methods or APIs available for Krita plugin development.

I’m getting a similar error when trying to export the script.

The error occurs because Python uses sys.executable to execute the other processes, which means it tries to run another Krita process. You have to use multiprocessing.set_executable to make it run a Python interpreter instead.