Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17699

Re: Why does this launch an infinite loop of new processes?

Date 2011-12-21 11:29 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Why does this launch an infinite loop of new processes?
References <4EF22CA1.1070200@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3957.1324497117.27778.python-list@python.org> (permalink)

Show all headers | View raw


Andrew Berg wrote:
> I am trying to understand the multiprocessing module, and I tried some
> simple code:
> 
> import multiprocessing
> def f():
> 	print('bla bla')
> p = multiprocessing.Process(target=f)
> p.start()
> p.join()
> 
> And the result is a new process that spawns a new process that spawns a
> new process ad infinitum until I log out and the OS forcefully
> terminates all my user processes. I have no idea what is happening; I
> expected it to just print the string and terminate.
> 

Anything that runs at import time should be protected by the `if 
__name__ == '__main__'` idiom as the children will import the __main__ 
module.


8<-----------------------------------------------
import multiprocessing
def f():
     print('bla bla')

if __name__ == '__main__':
     p = multiprocessing.Process(target=f)
     p.start()
     p.join()
8<-----------------------------------------------

~Ethan~

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Why does this launch an infinite loop of new processes? Ethan Furman <ethan@stoneleaf.us> - 2011-12-21 11:29 -0800

csiph-web