MCPcopy Index your code
hub / github.com/apache/cassandra-python-driver / add_callback

Method add_callback

cassandra/cluster.py:5058–5105  ·  view source on GitHub ↗

Attaches a callback function to be called when the final results arrive. By default, `fn` will be called with the results as the first and only argument. If `*args` or `**kwargs` are supplied, they will be passed through as additional positional or keyword argument

(self, fn, *args, **kwargs)

Source from the content-addressed store, hash-verified

5056 return trace
5057
5058 def add_callback(self, fn, *args, **kwargs):
5059 """
5060 Attaches a callback function to be called when the final results arrive.
5061
5062 By default, `fn` will be called with the results as the first and only
5063 argument. If `*args` or `**kwargs` are supplied, they will be passed
5064 through as additional positional or keyword arguments to `fn`.
5065
5066 If an error is hit while executing the operation, a callback attached
5067 here will not be called. Use :meth:`.add_errback()` or :meth:`add_callbacks()`
5068 if you wish to handle that case.
5069
5070 If the final result has already been seen when this method is called,
5071 the callback will be called immediately (before this method returns).
5072
5073 Note: in the case that the result is not available when the callback is added,
5074 the callback is executed by IO event thread. This means that the callback
5075 should not block or attempt further synchronous requests, because no further
5076 IO will be processed until the callback returns.
5077
5078 **Important**: if the callback you attach results in an exception being
5079 raised, **the exception will be ignored**, so please ensure your
5080 callback handles all error cases that you care about.
5081
5082 Usage example::
5083
5084 >>> session = cluster.connect("mykeyspace")
5085
5086 >>> def handle_results(rows, start_time, should_log=False):
5087 ... if should_log:
5088 ... log.info("Total time: %f", time.time() - start_time)
5089 ... ...
5090
5091 >>> future = session.execute_async("SELECT * FROM users")
5092 >>> future.add_callback(handle_results, time.time(), should_log=True)
5093
5094 """
5095 run_now = False
5096 with self._callback_lock:
5097 # Always add fn to self._callbacks, even when we're about to
5098 # execute it, to prevent races with functions like
5099 # start_fetching_next_page that reset _final_result
5100 self._callbacks.append((fn, args, kwargs))
5101 if self._final_result is not _NOT_SET:
5102 run_now = True
5103 if run_now:
5104 fn(self._final_result, *args, **kwargs)
5105 return self
5106
5107 def add_errback(self, fn, *args, **kwargs):
5108 """

Callers 1

add_callbacksMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected