defrun_in_independent_process(is_cuda:bool,fn,*args,**kwargs):"""make function run in another process(used to avoid memory leak or other bad side-effects)"""fromconcurrent.futuresimportProcessPoolExecutorimportmultiprocessingifis_cuda:cxt_name="forkserver"else:cxt_name="fork"# logger.info("Create independent process for function [%s] in [%s] mode", fn.__name__, cxt_name)cxt=multiprocessing.get_context(cxt_name)withProcessPoolExecutor(max_workers=1,mp_context=cxt)asp:future=p.submit(fn,*args,**kwargs)returnfuture.result()