[docs] [issue12954] Multiprocessing logging under Windows

paul j3 report at bugs.python.org
Wed Aug 13 05:52:43 CEST 2014


paul j3 added the comment:

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?

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12954>
_______________________________________


More information about the docs mailing list