Pyinstaller: EXE keep running when using multiprocessing package

Created on 5 Mar 2019  路  3Comments  路  Source: pyinstaller/pyinstaller

Everything is working great to form an EXE file. However, when executing exe file, it keeps running which is absolutely not normal. I think the problem is related to multiprocessing package. Is there any past experience?

`
import multiprocessing as mp
import threading as td
import time

def job(q):
res = 0
for i in range(1000000):
res += i+i2+i3
q.put(res) # queue

def multicore():
q = mp.Queue()
p1 = mp.Process(target=job, args=(q,))
p2 = mp.Process(target=job, args=(q,))
p1.start()
p2.start()
p1.join()
p2.join()
res1 = q.get()
res2 = q.get()
print('multicore:' , res1+res2)

def normal():
res = 0
for _ in range(2):
for i in range(1000000):
res += i+i2+i3
print('normal:', res)

def multithread():
q = mp.Queue()
t1 = td.Thread(target=job, args=(q,))
t2 = td.Thread(target=job, args=(q,))
t1.start()
t2.start()
t1.join()
t2.join()
res1 = q.get()
res2 = q.get()
print('multithread:', res1+res2)

if __name__ == '__main__':
st = time.time()
normal()
st1= time.time()
print('normal time:', st1 - st)
multithread()
st2 = time.time()
print('multithread time:', st2 - st1)
multicore()
print('multicore time:', time.time()-st2)
input() # Stop at finish
`

Most helpful comment

I had a similar issue, and the following thread was of great help #3907 and this one too #87.

All 3 comments

Happens to me as well.
I am using
_python_ 3.5.4
_pyinstaller_ 3.2.1

The behavior is that the python script is being executed again and again in a infinite loop.
When running the same code via python (and not via the pyinstaller .exe file) the scripts is being executed once and then it is being termintated(as expected).
When removing the _multiprocessing_ import(and dependant code), everything works perfectly and the script is being terminated once its done(both in python and exe).

I had a similar issue, and the following thread was of great help #3907 and this one too #87.

Thank you @davinellulinvega for the pointers to the fix. Specifically in my case the muliprocess import had to be the first import and then the freeze had to happen as the first thing in the if __name__ == '__main__': section:

from multiprocessing import freeze_support

... 

if __name__ == '__main__':
    freeze_support()
Was this page helpful?
0 / 5 - 0 ratings