Python's multiprocessing map_async generates error on Windows
Question: The code below works perfectly on Unix but generates a
multiprocessing.TimeoutError on Windows 7 (both OS use python 2.7).

Any idea why? Thanks.

    
    
    from multiprocessing import Pool
    
    def increment(x):
        return x + 1
    
    def decrement(x):
        return x - 1
    
    pool = Pool(processes=2)
    res1 = pool.map_async(increment, range(10))
    res2 = pool.map_async(decrement, range(10))
    
    print res1.get(timeout=1)
    print res2.get(timeout=1)
    


Answer: You need to put your actual program logic in side a `if __name__ ==
'__main__':` block.

On Unixy systems, Python forks, producing multiple processes to work from.
Windows doesn't have fork. Python has to launch a new interpreter and re-
import all your modules instead. This means that each subprocess will reimport
your main module. For the code you've written reimporting the module will
cause each newly launched processes to launch processes of its own.

See: <http://docs.python.org/library/multiprocessing.html#windows>

**EDIT** this works for me:

    
    
    from multiprocessing import Pool
    
    def increment(x):
        return x + 1
    
    def decrement(x):
        return x - 1
    
    if __name__ == '__main__':
        pool = Pool(processes=2)
        res1 = pool.map_async(increment, range(10))
        res2 = pool.map_async(decrement, range(10))
    
        print res1.get(timeout=1)
        print res2.get(timeout=1)
    

