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

Todd toddrjen at gmail.com
Thu Apr 30 11:48:21 CEST 2015


Looking at pep 492, it seems to me the handling of "for" loops has use
outside of just asyncio.  The primary use-case I can think of is
multiprocessing and multithreading.

For example, you could create a multiprocessing pool, and let the pool
handle the items in a "for" loop, like so:

    from multiprocessing import Pool

    mypool = Pool(10, maxtasksperchild=2)

    mypool for item in items:
        do_something_here
        do_something_else
        do_yet_another_thing



Or something similar with third-party modules:

   from greenlet import greenlet

   greenlet for item in items:
        do_something_here
        do_something_else
        do_yet_another_thing

Of course this sort of thing is possible with iterators and maps today, but
I think a lot of the same advantages that apply to asyncio also apply to
these sorts of cases.  So I think that, rather than having a special
keyword just for asyncio, I think it would be better to have a more
flexible approach.  Perhaps something like a "__for__" magic method that
lets a class implement "for" loop handling, along with the corresponding
changes in how the language processes the "for" loop.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150430/ddeb20d1/attachment.html>


More information about the Python-ideas mailing list