Hi, If you are on windows, I recall that we can’t use multiprocessing inside a function, it should be in the main body of code or after if ‘__name__’ == ‘__main__’ block.
Also, it is necessary to ensure that the function being used with multiprocessing can be pickled, if it cant be multiprocessing won’t work with it. On Tue, 6 May 2025 at 1:39 am, Ran Waidman via Python.NET < pythonnet@python.org> wrote: > Hi, I am using Python.Runtime in C#, > > I would like to call 1 function from my script, this function must work in > multip process. > when I call it, it looks like that the sw keep opening new processes > forever till my computer has to be restarted it is like a virus, > > my python code: > import math > import multiprocessing > import time > from multiprocessing import pool > > import cv2 > import numpy as np > import os > from PIL import Image > > from GraininessFunction import GraininessFunction > > > #multiprocessing.set_start_method('spawn', force=True) # or 'fork' on Unix > > def getString(): > > return "Hello from Python!" > > def worker(x): > print(f"Processing {x} in PID {os.getpid()}") > return x * x > > def multiProcessTest(): > multiprocessing.freeze_support() > #multiprocessing.freeze_support() > args = [1,2,3,4] > processes = [] > > print(f"multiProcessTest in PID {os.getpid()}") > > for item in args: > print("creating process for:"+str(item)) > p = multiprocessing.Process(target=worker, args=(item,)) > p.start() > processes.append(p) > > for p in processes: > p.join() > #with multiprocessing.Pool(processes=2) as pool: > # pool.map(worker, args) > > #multiprocessing.freeze_support() > #if multiprocessing.get_start_method(allow_none=True) != 'spawn': > # multiprocessing.set_start_method('spawn', force=True) > > > my C# code: > > var ret = module.getString(); > > Console.WriteLine($"Result from Python: {ret}"); > > module.multiProcessTest(); > > Console.WriteLine($"end multiProcessTest"); > _______________________________________________ > Python.NET mailing list -- pythonnet@python.org > To unsubscribe send an email to pythonnet-le...@python.org > https://mail.python.org/mailman3/lists/pythonnet.python.org/ > Member address: nikhilgarg....@gmail.com >
_______________________________________________ Python.NET mailing list -- pythonnet@python.org To unsubscribe send an email to pythonnet-le...@python.org https://mail.python.org/mailman3/lists/pythonnet.python.org/ Member address: arch...@mail-archive.com