[Python-ideas] More general "for" loop handling

Andrew Barnert abarnert at yahoo.com
Sat May 2 18:16:33 CEST 2015


On May 1, 2015, at 08:52, Joseph Martinot-Lagarde <joseph.martinot-lagarde at m4x.org> wrote:
> 
> Le 01/05/2015 02:35, Steven D'Aprano a écrit :
>> 
>> If you still wish to argue for this, one thing which may help your case
>> is if you can identify other programming languages that have already
>> done something similar.
> Cython has prange. It replaces range() in the for loop but runs the loop body in parallel using openmp:

I think that's pretty good evidence that this proposal (I meant the syntax for loop modifiers, not "some way to do loops in parallel would be nice") isn't needed. What OpenMP has to do with loop modifier syntax, Cython can do with just a special iterator in normal Python syntax.

Of course that doesn't guarantee that something similar to prange could be built for Python 3.5's Pool, Executor, etc. types without changes, but if even if it can't, a change to the iterator protocol to make prange bulldable doesn't seem as disruptive as a change to the basic syntax of the for loop. (Unless there just is no reasonable change to the protocol that could work.)

> from cython.parallel import prange
> 
> cdef int func(Py_ssize_t n):
>    cdef Py_ssize_t i
> 
>    for i in prange(n, nogil=True):
>        if i == 8:
>            with gil:
>                raise Exception()
>        elif i == 4:
>            break
>        elif i == 2:
>            return i
> 
> This is an example from the cython documentation: http://docs.cython.org/src/userguide/parallelism.html
> 
> Joseph
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list