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

Joseph Martinot-Lagarde joseph.martinot-lagarde at m4x.org
Sun May 3 23:52:32 CEST 2015


Le 02/05/2015 18:16, Andrew Barnert via Python-ideas a écrit :
> 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.

Cython uses python syntax but the behavior is different. This is 
especially obvious seeing how break and return are managed, where the 
difference in not only in the iterator.

>
> 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/
> _______________________________________________
> 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