Davin Potts added the comment:

There are at least two issues at play here.


Running the attached file on OS X produces starkly different results -- console 
prints:
CREATED TEMP DIRECTORY 
/var/folders/s4/tc1y5rjx25vfknpzvnfh1b140000gn/T/temp_z6I0BA
task1
task2
ATEXIT: REMOVING TEMP DIRECTORY 
/var/folders/s4/tc1y5rjx25vfknpzvnfh1b140000gn/T/temp_z6I0BA


The reason only one temp directory is created on OS X (or on other unix-y 
platforms) and more than one is created on Windows is described in more detail 
here:
https://docs.python.org/2/library/multiprocessing.html#windows

In short, on Windows 8.1, the processes you spawn via multiprocessing must 
import your main module ("task_spawn" in this case) and in so doing each 
executes both the line creating a temp directory and the lines following it 
(this is part of how import works).  I suspect you want instead to put these 
lines inside a "if __name__ == '__main__'" clause -- doing so will ensure only 
one temp dir is created and it will be properly cleaned up when the interpreter 
exits cleanly.  You will have consistent behavior across Windows and unix-y 
platforms this way too, not to mention your code will more clearly convey that 
you only want the main process to create a temp dir.  (Specifically see the 
section "Safe importing of main module" in the docs at the above link.)


That was the first issue -- on to the second.


The registering of functions with atexit means they'll be executed upon "normal 
interpreter termination".  Lifting a snippet from the atexit docs' introduction 
section (https://docs.python.org/2/library/atexit.html):

  Note: The functions registered via this module are not called when the 
program is killed by a signal not handled by Python, when a Python fatal 
internal error is detected, or when os._exit() is called.

When the processes created and managed via multiprocessing reach termination, 
that is quite different from "normal interpreter termination".  You are 
observing that when the interpreter's (main) process is done, it executes the 
function you registered with atexit -- that is how it should be.  Registering 
functions with atexit inside distinct processes will not cause them to be 
automagically registered with atexit in the parent interpreter process.



Hopefully with the above explanation in hand it will be possible to make the 
necessary changes to correct your code without breaking a sweat.

----------
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue23489>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to