This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author paul.j3
Recipients Ari.Koivula, BreamoreBoy, docs@python, paul.j3
Date 2014-08-13.03:52:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1407901963.69.0.610288949422.issue12954@psf.upfronthosting.co.za>
In-reply-to
Content
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?
History
Date User Action Args
2014-08-13 03:52:43paul.j3setrecipients: + paul.j3, docs@python, BreamoreBoy, Ari.Koivula
2014-08-13 03:52:43paul.j3setmessageid: <1407901963.69.0.610288949422.issue12954@psf.upfronthosting.co.za>
2014-08-13 03:52:43paul.j3linkissue12954 messages
2014-08-13 03:52:42paul.j3create