def call(self, method, *args, **kwargs): """ Send a websocket rpc method call, then wait for and return the eventual response, or raise an exception if we get back an error. This method will raise an AssertionError after 10 seconds if no response of any kind was received. The positional and keyword arguments to this method are used as the arguments to the rpc function call. """ result, error = [], [] callback = self._next_id('callback') self._callbacks[callback] = { 'callback': result.append, 'errback': error.append } try: self._send(method=method, params=args or kwargs, callback=callback) except: self._callbacks.pop(callback, None) raise for i in range(10 * config['ws_call_timeout']): stopped.wait(0.1) if stopped.is_set() or result or error: break self._callbacks.pop(callback, None) assert not stopped.is_set(), 'websocket closed before response was received' assert result, error[0] if error else 'no response received for 10 seconds' return result[0]
def call(self, method, *args, **kwargs): """ Send a websocket rpc method call, then wait for and return the eventual response, or raise an exception if we get back an error. This method will raise an AssertionError after 10 seconds if no response of any kind was received. The positional and keyword arguments to this method are used as the arguments to the rpc function call. """ result, error = [], [] callback = self._next_id('callback') self._callbacks[callback] = { 'callback': result.append, 'errback': error.append } try: self._send(method=method, params=args or kwargs, callback=callback) except: self._callbacks.pop(callback, None) raise for i in range(10 * config['ws.call_timeout']): stopped.wait(0.1) if stopped.is_set() or result or error: break self._callbacks.pop(callback, None) assert not stopped.is_set(), 'websocket closed before response was received' assert result, error[0] if error else 'no response received for 10 seconds' return result[0]
def connect(self, max_wait=0): """ Start the background threads which connect this websocket and handle RPC dispatching. This method is safe to call even if the websocket is already connected. You may optionally pass a max_wait parameter if you want to wait for up to that amount of time for the connection to go through; if that amount of time elapses without successfully connecting, a warning message is logged. """ self._checker.start() self._dispatcher.start() for i in range(10 * max_wait): if not self.connected: stopped.wait(0.1) else: break else: if max_wait: log.warn('websocket {!r} not connected after {} seconds', self.url, max_wait)