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

Paul Moore p.f.moore at gmail.com
Thu Apr 30 18:45:56 CEST 2015


On 30 April 2015 at 17:03, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Steven D'Aprano schrieb am 30.04.2015 um 13:36:
>> "async for" hasn't proven itself yet, and you are already looking to
>> generalise it? Shouldn't it prove itself as not a mistake first?
>
> Also, it should be quite possible to achieve what the OP proposed with
> "async for" since it's in no way limited to the way asyncio handles things.
> "async for" is a bit of a badly named feature, but that's intended in order
> to match what people would know from other programming languages.

Could you explain how?

Specifically, what's the translation of

from multiprocessing import Pool

    mypool = Pool(10, maxtasksperchild=2)

    mypool for item in items:
        do_something_here
        do_something_else
        do_yet_another_thing

I'm assuming that's the OP's intention (it's certainly mine) is that
the "mypool for" loop works something like

    def _work(item):
        do_something_here
        do_something_else
        do_yet_another_thing
    for _ in mypool.map(_work, items):
        # Wait for the subprocesses
        pass

How would I use "async for" to get the same result? (And the same for
a concurrent.futures Executor in place of a multiprocessing pool).

Paul.


More information about the Python-ideas mailing list