[Python-ideas] The async API of the future: yield-from

Terry Reedy tjreedy at udel.edu
Sun Oct 14 19:27:34 CEST 2012


On 10/14/2012 10:36 AM, Guido van Rossum wrote:

> So, can par() be as simple as
>
> def par(*args):
>    results = []
>    for task in args:
>      result = yield from task
>      results.append(result)
>    return results
>
> ???
>
> Or does it need to interact with the scheduler to ensure fairness?
> (Not having built one of these, my intuition for how the primitives
> fit together is still lacking, so excuse me for asking naive
> questions.)
>
> Of course there's the question of what to do when one of the tasks
> raises an error -- I haven't quite figured that out in NDB either, it
> runs all the tasks to completion but the caller only sees the first
> exception. I briefly considered having an "multi-exception" but it
> felt too weird -- though I'm not married to that decision.

One answer is to append the exception object to results and let the 
requesting code sort out what to do.

def par(*args):
    results = []
    for task in args:
       try:
          result = yield from task
          results.append(result)
       except Exception as exc:
          results.append(exc)
    return results


-- 
Terry Jan Reedy




More information about the Python-ideas mailing list