[Python-Dev] PEP 377 : Allow __enter__() methods to skip the statement body : real world case

Pierre Chanial pierre.chanial at gmail.com
Fri May 4 14:17:56 CEST 2012


Hello,

PEP 377 has been rejected for lack of use cases. Here is one.

I'm writing an MPI-based application 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. This new communicator should finally
be freed by the processes that had work to do (and only by them). If there
is more work than processes, the usual communicator should be used.

A neat way to do that would be to write:

with filter_comm(comm, nworkitems) as newcomm:
    ... do work with communicator newcomm...

the body being executed only by the processes that have work to do.

It looks better than:

if comm.size < nworkitems:
    newcomm = get_new_communicator(comm, nworkitems)
else:
    newcomm = comm

if comm.rank < nworkitems:
    try:
        ... do work with communicator newcomm...
    finally:
        if comm.size < nworkitems:
            newcomm.Free()

Especially since I have to use that quite often.

Cheers,

Pierre
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120504/de2d9ba6/attachment.html>


More information about the Python-Dev mailing list