Python:协程 - StopIteration 异常
Python: Coroutines - StopIteration exception
我正在尝试使用 .send()
来提供字典。我的代码片段如下
def coroutine(func):
def start(*args, **kwargs):
cr = func(*args, **kwargs)
next(cr)
return cr
return start
@coroutine
def putd(di):
print("via coroutines adding a key : value to dictionary")
try:
item = yield
for key, value in item.items():
if key in di:
print("Key : {0} already exists".format(key))
else:
di[key] = value
print(di)
except StopIteration :
print("yield frame got closed")
di = {}
gobj = putd(di)
gobj.send({"plan" : "shuttle"})
gobj.close()
而且我相信我正在正确处理 exception
但我仍然遇到 StopIteration
异常。
scratch.py
Traceback (most recent call last):
via coroutines adding a key : value to dictionary
{'plan': 'shuttle'}
File "scratch.py", line 39, in <module>
gobj.send({"plan" : "shuttle"})
StopIteration
Process finished with exit code 1
我是不是没有正确处理该异常,还是遗漏了什么?非常感谢任何帮助。
您的协程在第一个 send/yield 后退出。这会生成一个 StopIteration
并且您不能在协程本身中处理它,但只能在调用 send
时处理它。来自文档:
The send() method returns the next value yielded by the generator, or
raises StopIteration if the generator exits without yielding another
value.
@coroutine
def putd(di):
print("via coroutines adding a key : value to dictionary")
try:
item = yield
for key, value in item.items():
if key in di:
print("Key : {0} already exists".format(key))
else:
di[key] = value
print(di)
except StopIteration :
print("yield frame got closed")
# here is an implicit return None which terminates the coroutine
我猜你想让协程保持活动状态,接受尽可能多的发送,直到显式 close:
@coroutine
def putd(di):
print("via coroutines adding a key : value to dictionary")
try:
while True:
item = yield
for key, value in item.items():
if key in di:
print("Key : {0} already exists".format(key))
else:
di[key] = value
print(di)
except GeneratorExit:
print("yield frame got closed")
请注意,现在捕获了 GeneratorExit
异常。
我正在尝试使用 .send()
来提供字典。我的代码片段如下
def coroutine(func):
def start(*args, **kwargs):
cr = func(*args, **kwargs)
next(cr)
return cr
return start
@coroutine
def putd(di):
print("via coroutines adding a key : value to dictionary")
try:
item = yield
for key, value in item.items():
if key in di:
print("Key : {0} already exists".format(key))
else:
di[key] = value
print(di)
except StopIteration :
print("yield frame got closed")
di = {}
gobj = putd(di)
gobj.send({"plan" : "shuttle"})
gobj.close()
而且我相信我正在正确处理 exception
但我仍然遇到 StopIteration
异常。
scratch.py
Traceback (most recent call last):
via coroutines adding a key : value to dictionary
{'plan': 'shuttle'}
File "scratch.py", line 39, in <module>
gobj.send({"plan" : "shuttle"})
StopIteration
Process finished with exit code 1
我是不是没有正确处理该异常,还是遗漏了什么?非常感谢任何帮助。
您的协程在第一个 send/yield 后退出。这会生成一个 StopIteration
并且您不能在协程本身中处理它,但只能在调用 send
时处理它。来自文档:
The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value.
@coroutine
def putd(di):
print("via coroutines adding a key : value to dictionary")
try:
item = yield
for key, value in item.items():
if key in di:
print("Key : {0} already exists".format(key))
else:
di[key] = value
print(di)
except StopIteration :
print("yield frame got closed")
# here is an implicit return None which terminates the coroutine
我猜你想让协程保持活动状态,接受尽可能多的发送,直到显式 close:
@coroutine
def putd(di):
print("via coroutines adding a key : value to dictionary")
try:
while True:
item = yield
for key, value in item.items():
if key in di:
print("Key : {0} already exists".format(key))
else:
di[key] = value
print(di)
except GeneratorExit:
print("yield frame got closed")
请注意,现在捕获了 GeneratorExit
异常。