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

Veek. M vek.m1234 at gmail.com
Mon Feb 8 04:17:02 EST 2016


****************************
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?



More information about the Python-list mailing list