def dispatch(self, msg: ParsedMessagePayload) -> None: id = msg.get("id") if id: callback = self._callbacks.pop(id) if callback.future.cancelled(): return error = msg.get("error") if error: parsed_error = parse_error(error["error"]) # type: ignore parsed_error.stack = "".join( traceback.format_list(callback.stack_trace)[-10:]) callback.future.set_exception(parsed_error) else: result = self._replace_guids_with_channels(msg.get("result")) callback.future.set_result(result) return guid = msg["guid"] method = msg.get("method") params = msg.get("params") if method == "__create__": assert params parent = self._objects[guid] self._create_remote_object(parent, params["type"], params["guid"], params["initializer"]) return object = self._objects.get(guid) if not object: raise Exception(f'Cannot find object to "{method}": {guid}') if method == "__adopt__": child_guid = cast(Dict[str, str], params)["guid"] child = self._objects.get(child_guid) if not child: raise Exception(f"Unknown new child: {child_guid}") object._adopt(child) return if method == "__dispose__": self._objects[guid]._dispose() return object = self._objects[guid] try: if self._is_sync: for listener in object._channel.listeners(method): # Each event handler is a potentilly blocking context, create a fiber for each # and switch to them in order, until they block inside and pass control to each # other and then eventually back to dispatcher as listener functions return. g = greenlet(listener) g.switch(self._replace_guids_with_channels(params)) else: object._channel.emit(method, self._replace_guids_with_channels(params)) except BaseException as exc: print("Error occured in event listener", file=sys.stderr) traceback.print_exc() self._error = exc
def _dispatch(self, msg: ParsedMessagePayload) -> None: id = msg.get("id") if id: callback = self._callbacks.pop(id) if callback.future.cancelled(): return error = msg.get("error") if error: parsed_error = parse_error(error["error"]) # type: ignore parsed_error.stack = "".join( traceback.format_list(callback.stack_trace)[-10:] ) callback.future.set_exception(parsed_error) else: result = self._replace_guids_with_channels(msg.get("result")) callback.future.set_result(result) return guid = msg["guid"] method = msg.get("method") params = msg["params"] if method == "__create__": parent = self._objects[guid] self._create_remote_object( parent, params["type"], params["guid"], params["initializer"] ) return if method == "__dispose__": self._objects[guid]._dispose() return object = self._objects[guid] try: if self._is_sync: for listener in object._channel.listeners(method): g = greenlet(listener) g.switch(self._replace_guids_with_channels(params)) else: object._channel.emit(method, self._replace_guids_with_channels(params)) except Exception: print( "Error dispatching the event", "".join(traceback.format_exception(*sys.exc_info())), )