[Python-ideas] Async API

Yury Selivanov yselivanov.ml at gmail.com
Sun Oct 28 02:29:31 CEST 2012


On 2012-10-27, at 7:52 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

> Yury Selivanov wrote:
>> But now I'm not sure this approach will work with yield-froms.
>> As when you yield-fromming scheduler knows nothing about the chain of generators, as it's all hidden in the yield-from implementation.
> 
> I think this just means that the implementation would involve
> more than looking at a single bit. Something like an in_finally()
> method that looks along the yield-from chain and returns true if
> any of the generators are in a finally section.

That would not be a solution either.

Imagine that we have two coroutines:

  @coroutine
  def c1():
      try:
          yield c2().with_timeout(1.0)      # p1
      finally:
          try:
              yield c2().with_timeout(1.0)  # p2
          except TimeoutError:
              pass

  @coroutine
  def c2():
      try:
          yield c3().with_timeout(2.0)      # p3
      finally:
          yield c4()                        # p4
         
In the above example scheduler *can* safely interrupt "c2" when it
is invoked from "c1" at "p2".  I.e. scheduler can't interrupt the
coroutine when it is itself in its finally statement, but it's fine
to interrupt it when it is not, even if it is invoked from other
coroutine's finally block.

If you translate this example in yield-from form, then checking 
'in_finally()' result on "c1" when it is at "p2" will prevent you
to raise TimeoutError, but you clearly should.

In other words, we want coroutines behaviour to be closer to the
regular python code.

-
Yury





More information about the Python-ideas mailing list