Unexpected behaviour in python multiprocessing
Question: I'm trying to understand the following odd behavior observed using the `python
mutiprocessing`.

Sample testClass: import os import multiprocessing

    
    
    class testClass(multiprocessing.Process):    
    
        def __del__(self):
            print "__del__ PID: %d" % os.getpid()
            print self.a    
    
        def __init__(self):
            multiprocessing.Process.__init__(self)
            print "__init__ PID: %d" % os.getpid()
            self.a = 0    
    
        def run(self):
            print "method1 PID: %d" % os.getpid()
            self.a = 1
    

And a little test program: from testClass import testClass

    
    
    print "Start"
    proc_list = []
    proc_list.append(testClass())
    proc_list[-1].start()    
    
    proc_list[-1].join()    
    
    print "End"
    

This produces:

    
    
    Start
    __init__ PID: 89578
    method1 PID: 89585
    End
    __del__ PID: 89578
    0
    

Why it does not print `1`?

I'm guessing that it's related to the fact that `run` is actually being
executed on a different process as can be seen. If this is the expected
behavior how is everyone using multiprocessing where processes have an
expensive `__init__` as in processes that need to open a database?

And shouldn't this behaviour be better highlighted in multiprocessing
documentation?


Answer: You can wrap your expensive initialization inside a context manager:

    
    
    def run(self):
        with expensive_initialization() as initialized_object:
            do_some_logic_here(initialized_object)
    

You will have a chance to properly initialize your object before calling
`do_some_logic_here`, and to properly release the resources after leaving the
context manager's block.

See
[documentation](https://docs.python.org/3.6/reference/datamodel.html#context-
managers).

