coroutine, throw, yield, call-stack and exception handling

Veek. M vek.m1234 at gmail.com
Mon Feb 8 04:34:55 EST 2016


Veek. M wrote:

> ****************************
> Exceptions can be raised inside a coroutine using the throw(
> 
> Exceptions raised in this manner will originate at the currently
> executing yield state-ment in the coroutine.A coroutine can elect to
> catch exceptions and handle them as appropriate. It is not safe to use
> throw() as an asynchronous signal to a coroutine—it should never be
> invoked from a separate execution thread or in a signal handler.
> ****************************
> 
> What does Beazley mean by this: 'will originate at the currently
> executing yield state-ment in the coroutine'
> 
> If he's throw'ing an exception surely it originates at the throw:
> 
> def mycoroutine():
>  while len(n) > 2:
>    n = (yield)
> 
>  throw('RuntimeError' "die!")
> ----------------------
> Also: 'not safe to use throw() as an asynchronous signal to a
> coroutine— it should never be invoked from a separate execution thread
> or in a signal handler.'
> 
> You can use throw within a coroutine to raise an exception.
> How would you use it as an async-sig to a coroutine..
> eg: you have two threads
> 1. coroutine does except FooException:
> 2. throw(FooException, 'message')
> 
> so moment 'throw' runs and an exception is raised.. it'll propagate
> within thread-2 to its parent etc - how is thread-1 affected?
Veek. M wrote:

> ****************************
> Exceptions can be raised inside a coroutine using the throw(
> 
> Exceptions raised in this manner will originate at the currently
> executing yield state-ment in the coroutine.A coroutine can elect to
> catch exceptions and handle them as appropriate. It is not safe to use
> throw() as an asynchronous signal to a coroutine—it should never be
> invoked from a separate execution thread or in a signal handler.
> ****************************
> 
> What does Beazley mean by this: 'will originate at the currently
> executing yield state-ment in the coroutine'
> 
> If he's throw'ing an exception surely it originates at the throw:
> 
> def mycoroutine():
>  while len(n) > 2:
>    n = (yield)
> 
>  throw('RuntimeError' "die!")
> ----------------------
> Also: 'not safe to use throw() as an asynchronous signal to a
> coroutine— it should never be invoked from a separate execution thread
> or in a signal handler.'
> 
> You can use throw within a coroutine to raise an exception.
> How would you use it as an async-sig to a coroutine..
> eg: you have two threads
> 1. coroutine does except FooException:
> 2. throw(FooException, 'message')
> 
> so moment 'throw' runs and an exception is raised.. it'll propagate
> within thread-2 to its parent etc - how is thread-1 affected?

Also this bit:
***********************
If a coroutine returns values, some care is required if exceptions 
raised with throw() are being handled. If you raise an exception in a 
coroutine using throw(), the value passed to the next yield in the 
coroutine will be returned as the result of throw(). If
you need this value and forget to save it, it will be lost.
***********************

def coroutine():
  while True:
   line = (yield result)

  throw(FooException)

where is the question of a 'yield'? You'll exit the coroutine straight 
away..



More information about the Python-list mailing list