[Python.NET] Re: multiprocessing in Python.Runtime

"Nikhil Garg via Python.NET" <[email protected]> Tue, 6 May 2025 08:07:50 +1000
Newsgroups gmane.comp.python.dotnet
Message-ID <CAL3fuhk=5b+UTQ4-q=F0cAFNt6+JieV7E3G_Ks5eG0+VY5p5pQ@mail.gmail.com>
--===============5913788242282794561==
Content-Type: multipart/alternative; boundary="000000000000bf0f7806346abb58"

--000000000000bf0f7806346abb58
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Hi,

If you are on windows, I recall that we can=E2=80=99t use multiprocessing i=
nside a
function, it should be in the main body of code or after if =E2=80=98__name=
__=E2=80=99 =3D=3D
=E2=80=98__main__=E2=80=99 block.

Also, it is necessary to ensure that the function being used with
multiprocessing can be pickled, if it cant be multiprocessing won=E2=80=99t=
 work
with it.



On Tue, 6 May 2025 at 1:39=E2=80=AFam, Ran Waidman via Python.NET <
[email protected]> wrote:

> Hi, I am using Python.Runtime in C#,
>
> I would like to call 1 function from my script, this function must work i=
n
> 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=3DTrue)  # 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 =3D [1,2,3,4]
>     processes =3D []
>
>     print(f"multiProcessTest in PID {os.getpid()}")
>
>     for item in args:
>         print("creating process for:"+str(item))
>         p =3D multiprocessing.Process(target=3Dworker, args=3D(item,))
>         p.start()
>         processes.append(p)
>
>     for p in processes:
>         p.join()
>     #with multiprocessing.Pool(processes=3D2) as pool:
>     #    pool.map(worker, args)
>
> #multiprocessing.freeze_support()
> #if multiprocessing.get_start_method(allow_none=3DTrue) !=3D 'spawn':
> #    multiprocessing.set_start_method('spawn', force=3DTrue)
>
>
> my C# code:
>
>                     var ret =3D module.getString();
>
>                     Console.WriteLine($"Result from Python: {ret}");
>
>                     module.multiProcessTest();
>
>                     Console.WriteLine($"end multiProcessTest");
> _______________________________________________
> Python.NET mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/pythonnet.python.org/
> Member address: [email protected]
>

--000000000000bf0f7806346abb58
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto">Hi,</div><div dir=3D"auto"><br></div><div dir=3D"auto">If=
 you are on windows, I recall that we can=E2=80=99t use multiprocessing ins=
ide a function, it should be in the main body of code or after if =E2=80=98=
__name__=E2=80=99 =3D=3D =E2=80=98__main__=E2=80=99 block.=C2=A0</div><div =
dir=3D"auto"><br></div><div dir=3D"auto">Also, it is necessary to ensure th=
at the function being used with multiprocessing can be pickled, if it cant =
be multiprocessing won=E2=80=99t work with it.=C2=A0<br><div dir=3D"auto"><=
div dir=3D"ltr" class=3D"gmail_signature" data-smartmail=3D"gmail_signature=
"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div =
dir=3D"ltr"><div dir=3D"ltr"><font face=3D"monospace, monospace"></font><di=
v dir=3D"auto"><span style=3D"color:rgb(37,37,37);font-family:sans-serif;fo=
nt-size:14px;line-height:22.4px"><br></span></div></div></div></div></div><=
/div></div></div></div></div><div><br></div><div><br><div class=3D"gmail_qu=
ote gmail_quote_container"><div dir=3D"ltr" class=3D"gmail_attr">On Tue, 6 =
May 2025 at 1:39=E2=80=AFam, Ran Waidman via Python.NET &lt;<a href=3D"mail=
to:[email protected]">[email protected]</a>&gt; wrote:<br></div><bloc=
kquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #cc=
c solid;padding-left:1ex">Hi, I am using Python.Runtime in C#,<br>
<br>
I would like to call 1 function from my script, this function must work in =
multip process.<br>
when I call it, it looks like that the sw keep opening new processes foreve=
r till my computer has to be restarted it is like a virus,<br>
<br>
my python code:<br>
import math<br>
import multiprocessing<br>
import time<br>
from multiprocessing import pool<br>
<br>
import cv2<br>
import numpy as np<br>
import os<br>
from PIL import Image<br>
<br>
from GraininessFunction import GraininessFunction<br>
<br>
<br>
#multiprocessing.set_start_method(&#39;spawn&#39;, force=3DTrue)=C2=A0 # or=
 &#39;fork&#39; on Unix<br>
<br>
def getString():<br>
<br>
=C2=A0 =C2=A0 return &quot;Hello from Python!&quot;<br>
<br>
def worker(x):<br>
=C2=A0 =C2=A0 print(f&quot;Processing {x} in PID {os.getpid()}&quot;)<br>
=C2=A0 =C2=A0 return x * x<br>
<br>
def multiProcessTest():<br>
=C2=A0 =C2=A0 multiprocessing.freeze_support()<br>
=C2=A0 =C2=A0 #multiprocessing.freeze_support()<br>
=C2=A0 =C2=A0 args =3D [1,2,3,4]<br>
=C2=A0 =C2=A0 processes =3D []<br>
<br>
=C2=A0 =C2=A0 print(f&quot;multiProcessTest in PID {os.getpid()}&quot;)<br>
<br>
=C2=A0 =C2=A0 for item in args:<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 print(&quot;creating process for:&quot;+str(ite=
m))<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 p =3D multiprocessing.Process(target=3Dworker, =
args=3D(item,))<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 p.start()<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 processes.append(p)<br>
<br>
=C2=A0 =C2=A0 for p in processes:<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 p.join()<br>
=C2=A0 =C2=A0 #with multiprocessing.Pool(processes=3D2) as pool:<br>
=C2=A0 =C2=A0 #=C2=A0 =C2=A0 pool.map(worker, args)<br>
<br>
#multiprocessing.freeze_support()<br>
#if multiprocessing.get_start_method(allow_none=3DTrue) !=3D &#39;spawn&#39=
;:<br>
#=C2=A0 =C2=A0 multiprocessing.set_start_method(&#39;spawn&#39;, force=3DTr=
ue)<br>
<br>
<br>
my C# code:<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 var r=
et =3D module.getString();<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Conso=
le.WriteLine($&quot;Result from Python: {ret}&quot;);<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 modul=
e.multiProcessTest();<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Conso=
le.WriteLine($&quot;end multiProcessTest&quot;);<br>
_______________________________________________<br>
Python.NET mailing list -- <a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]=
g" target=3D"_blank">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman3/lists/pythonnet.python.org/" re=
l=3D"noreferrer" target=3D"_blank">https://mail.python.org/mailman3/lists/p=
ythonnet.python.org/</a><br>
Member address: <a href=3D"mailto:[email protected]" target=3D"_blan=
k">[email protected]</a><br>
</blockquote></div></div>

--000000000000bf0f7806346abb58--

--===============5913788242282794561==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Python.NET mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/pythonnet.python.org/
Member address: [email protected]

--===============5913788242282794561==--