The following python script:
import multiprocessing as mp
def f():
return None
def main():
print('create')
p = mp.Process(target=f)
print('start')
p.start()
print('join')
p.join()
print('done')
if __name__ == '__main__':
main()
Produces the following output in Python IDLE:
Python 3.7.6 (default, Jan 30 2020, 09:44:41)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license()" for more information.
>>>
===================== RESTART: /home/user/Desktop/test.py =====================
create
start
join
Followed by an (unexpected!) GUI prompt "Your program is still running! Do you want to kill it?"
Any clue what's going on here? I know printing from child processes is complicated in IDLE, but this seems unrelated.
Note that running the same code from the terminal gives "normal" output, and exits normally:
[user@localhost Desktop]$ python test.py
create
start
join
done
[user@localhost Desktop]$ python --version
Python 3.7.6
[user@localhost Desktop]$
At a suggestion from a colleague, I tried adding mp.set_start_method('spawn') to the if __name__ == '__main__': block:
import multiprocessing as mp
def f():
return None
def main():
mp.set_start_method('spawn')
print('create')
p = mp.Process(target=f)
print('start')
p.start()
print('join')
p.join()
print('done')
if __name__ == '__main__':
main()
Which causes IDLE to behave how I hoped:
Python 3.7.6 (default, Jan 30 2020, 09:44:41)
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license()" for more information.
>>>
===================== RESTART: /home/user/Desktop/test.py =====================
create
start
join
done
>>>