vici: Catch Python GeneratorExit to properly cancel streamed event iteration

This commit is contained in:
Martin Willi 2015-03-09 12:06:38 +01:00
parent 288b654173
commit 90c5b48c96
2 changed files with 12 additions and 1 deletions

View File

@ -909,6 +909,11 @@ event:
for key in conn:
print key
Please note that if the returned generator is not iterated completely, it must
be closed using _close()_. This is implicitly done when breaking from a loop,
but an explicit call may be required when directly iterating the generator with
_next()_.
## Sorting in dictionaries ##
In VICI, in some message trees the order of objects in dictionary matters. In

View File

@ -281,10 +281,16 @@ class SessionHandler(object):
# issue command, and read any event messages
packet = Packet.request(command, message)
self.transport.send(packet)
exited = False
while True:
response = Packet.parse(self.transport.receive())
if response.response_type == Packet.EVENT:
yield Message.deserialize(response.payload)
if not exited:
try:
yield Message.deserialize(response.payload)
except GeneratorExit:
exited = True
pass
else:
break