def run(self): while True: try: data = yield from self.websocket.recv() except websockets.ConnectionClosed: break self._on_data(data) self.close()