Julius Unverfehrt 561a7f527c Pull request #36: RED-4206 wrap queue callback in process to manage memory allocation with the operating system and force deallocation after processing.
Merge in RR/image-prediction from RED-4206-fix-unwanted-restart-bug to master

Squashed commit of the following:

commit 3dfe7b861816ef9019103e16a23efd97a08fb617
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date:   Thu Sep 22 13:53:32 2022 +0200

    RED-4206 wrap queue callback in process to manage memory allocation with the operating system and force deallocation after processing.
2022-09-22 13:56:44 +02:00

26 lines
650 B
Python

import logging
import multiprocessing
logger = logging.getLogger("main")
def wrap_in_process(fn):
manager = multiprocessing.Manager()
return_queue = manager.list()
def process_fn(*args, **kwargs):
return_queue.append(fn(*args, **kwargs))
def wrapped_fn(*args, **kwargs):
logger.debug("Starting new subprocess")
process = multiprocessing.Process(target=process_fn, args=args, kwargs=kwargs)
process.start()
process.join()
try:
return return_queue.pop(0)
except IndexError:
logger.warning("No results returned by subprocess.")
return wrapped_fn