Skip to content

Commit

Permalink
Properly report publish writes as completed, even if there are
Browse files Browse the repository at this point in the history
multiple listening connections.

You can't directly capture the loop variable into a closure in python.
Also, we were erroneously passing an additional argument to
asyncio.Future's constructor when sub-classing it.
  • Loading branch information
jpieper committed Jun 8, 2014
1 parent 7999127 commit fa84c37
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions pygazebo/pygazebo.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def remove(self):

class WriteFuture(asyncio.Future):
def __init__(self, publisher, connections):
super(Publisher.WriteFuture, self).__init__(self)
super(Publisher.WriteFuture, self).__init__()

self.publisher = publisher
self.connections = dict((x, True) for x in connections)
Expand All @@ -102,7 +102,8 @@ def handle_done(self, future, connection):

try:
future.result()
except:
except Exception as e:
logger.debug('write error, closing connection:', str(e))
if connection in self.publisher._listeners:
self.publisher._listeners.remove(connection)

Expand All @@ -117,7 +118,8 @@ def _publish_impl(self, message):
for connection in self._listeners:
future = connection.write(message)
future.add_done_callback(
lambda future: result.handle_done(future, connection))
lambda future, connection=connection: result.handle_done(
future, connection))

return result

Expand Down
4 changes: 3 additions & 1 deletion tests/test_pygazebo.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,14 @@ def test_send(self, manager):

sample_message = gz_string_pb2.GzString()
sample_message.data = 'testdata'
publisher.publish(sample_message)
publish_future = publisher.publish(sample_message)

loop.run_until_complete(read_data1)
data_frame = read_data1.result()
assert data_frame == sample_message.SerializeToString()

assert loop.run_until_complete(publish_future) is None

# Test sending a very large message, it should require no
# individual writes which are too large.
read_data2 = asyncio.Future()
Expand Down

0 comments on commit fa84c37

Please sign in to comment.