Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102650 > unrolled thread
| Started by | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| First post | 2016-02-08 14:47 +0530 |
| Last post | 2016-02-09 17:25 +0530 |
| Articles | 4 — 2 participants |
Back to article view | Back to comp.lang.python
coroutine, throw, yield, call-stack and exception handling "Veek. M" <vek.m1234@gmail.com> - 2016-02-08 14:47 +0530
Re: coroutine, throw, yield, call-stack and exception handling "Veek. M" <vek.m1234@gmail.com> - 2016-02-08 15:04 +0530
Re: coroutine, throw, yield, call-stack and exception handling Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-08 14:56 -0700
Re: coroutine, throw, yield, call-stack and exception handling "Veek. M" <vek.m1234@gmail.com> - 2016-02-09 17:25 +0530
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-02-08 14:47 +0530 |
| Subject | coroutine, throw, yield, call-stack and exception handling |
| Message-ID | <n99m93$lc7$1@dont-email.me> |
****************************
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?
[toc] | [next] | [standalone]
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-02-08 15:04 +0530 |
| Message-ID | <n99nai$ols$1@dont-email.me> |
| In reply to | #102650 |
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..
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2016-02-08 14:56 -0700 |
| Message-ID | <mailman.112.1454968664.2317.python-list@python.org> |
| In reply to | #102650 |
On Mon, Feb 8, 2016 at 2:17 AM, Veek. M <vek.m1234@gmail.com> 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!")
The "throw" is not called from inside the coroutine. It's a method of
the generator object, and it's used by the calling code. It's similar
to calling the send method, except that instead of passing a value to
be returned by the yield expression, it passes an exception to be
raised inside the coroutine at the yield expression.
Example:
def mycoroutine():
n = 0
while True:
try:
n = (yield n)
except SomeException:
n = 42
coro = mycoroutine()
coro.next()
for i in range(100):
if i % 6 == 0:
coro.send(i % 6)
else:
coro.throw(SomeException())
> 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..
Taking my example from above, after SomeException is caught, the next
value yielded inside the coroutine will be the return value of the
coro.throw() call. This may be surprising if you're only expecting
coro.send() and not coro.throw() to return yielded values.
[toc] | [prev] | [next] | [standalone]
| From | "Veek. M" <vek.m1234@gmail.com> |
|---|---|
| Date | 2016-02-09 17:25 +0530 |
| Message-ID | <n9cjut$n97$1@dont-email.me> |
| In reply to | #102694 |
Ian Kelly wrote:
> On Mon, Feb 8, 2016 at 2:17 AM, Veek. M <vek.m1234@gmail.com> 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!")
>
> The "throw" is not called from inside the coroutine. It's a method of
> the generator object, and it's used by the calling code. It's similar
> to calling the send method, except that instead of passing a value to
> be returned by the yield expression, it passes an exception to be
> raised inside the coroutine at the yield expression.
>
> Example:
>
> def mycoroutine():
> n = 0
> while True:
> try:
> n = (yield n)
> except SomeException:
> n = 42
>
> coro = mycoroutine()
> coro.next()
> for i in range(100):
> if i % 6 == 0:
> coro.send(i % 6)
> else:
> coro.throw(SomeException())
>
>
>> 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..
>
> Taking my example from above, after SomeException is caught, the next
> value yielded inside the coroutine will be the return value of the
> coro.throw() call. This may be surprising if you're only expecting
> coro.send() and not coro.throw() to return yielded values.
Thanks, that made it abundantly clear :)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web