Python context manager: conditionnally executing body?
<p>I'm writing an MPI-based application (but MPI doesn't matter in my question, I mention it only to expose the rationale) and in some cases, when there is less work items than processes, I need to create a new communicator excluding the processes that have nothing to do. Finally, the new communicator has to be freed by the processes that have work to do (and only by them).</p>

<p>A neat way to do that would be to write:</p>

<pre><code>with filter_comm(comm, nworkitems) as newcomm:
    ... do work with communicator newcomm...
</code></pre>

<p>the body being executed only by the processes that have work to do.</p>

<p>Is there a way in a context manager to avoid executing the body?
I understand that context managers have rightfully been designed to avoid hiding control flows, but I wonder if it is possible to circumvent that, since in my case I think it would be justified for clarity sake.</p>
