Why the Pool class in multiprocessing lack the __exit__() method in Python 2?
<p>It is not clear to me why the <a href="http://fossies.org/dox/Python-2.7.12/pool_8py_source.html" rel="nofollow">Python 2.7 implementation</a> of <code>Pool</code> does not have the <code>__exit__()</code> method that is present in the <a href="http://fossies.org/dox/Python-3.5.2/pool_8py_source.html" rel="nofollow">Python 3 version</a> of the same class. Is it safe to add the <code>__exit__()</code> method (together with <code>__enter__()</code>, of course) (I just want to use <code>with Pool(n) as p:</code> ) or is there a special reason to avoid it? </p>

<p>Doesn't seem like there's any reason to avoid it. Looking at it and testing it real quick didn't bring up any odd behavior. This was implemented in <a href="http://bugs.python.org/issue15064" rel="nofollow"><em>Issue 15064</em></a>, it just seems it wasn't added in <code>2.7</code> (probably because only bug-fixes were considered).</p>

<p>Returning <code>self</code> from <code>__enter__</code> and calling <code>terminate</code> from <code>__exit__</code> as implemented in Python 3.3 should be the way to go. Instead of altering the source though (<em>if</em> that was your intention), just create a custom subclass:</p>

<pre><code>from multiprocessing.pool import Pool as PoolCls

class CMPool(PoolCls):
    def __enter__(self):
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        return self.terminate()
</code></pre>

python python-2.7 python-3.x threadpool python-multiprocessing