Message225253
I added a print line to a 'windows' example from the documentation:
from multiprocessing import Process
print 'importing multiprocessing'
def foo():
print 'hello'
p = Process(target=foo)
p.start()
Run with Python 2.7.0 on linux I get
importing multiprocessing
hello
Run with same, but on Windows I get
importing multiprocessing
importing multiprocessing
hello
importing multiprocessing
hello
(recursively)
Now if I put the last part into an if:
if __name__ == '__main__':
p = Process(target=foo)
p.start()
the Windows version no longer recurses, but I still get the double print message.
In linux the child process is created with `os.fork`, which makes a copy of the parent. The script is only loaded and run once.
In windows, the child is created by issuing a new call to Python with the script. The script is loaded and run by the child as well as the parent, hence the double print.
So any action that you don't want run when the child is created should be in the 'if __name__' block.
I can picture modifying the log_to_stderr function so that it checks the logger's 'handlers' list for one that already writes to stderr. It should be easy to add to your own code. But isn't it easier just to segregate all the 'main' actions from the 'child' ones? |
|
Date |
User |
Action |
Args |
2014-08-13 03:52:43 | paul.j3 | set | recipients:
+ paul.j3, docs@python, BreamoreBoy, Ari.Koivula |
2014-08-13 03:52:43 | paul.j3 | set | messageid: <1407901963.69.0.610288949422.issue12954@psf.upfronthosting.co.za> |
2014-08-13 03:52:43 | paul.j3 | link | issue12954 messages |
2014-08-13 03:52:42 | paul.j3 | create | |
|