Live.add_callback
Add a callback to the live client. This callback must take a single record argument. Refer to the What's a schema article for documentation on the fields contained with each record type.
A callback will receive error messages from the gateway with the ErrorMsg record type. This indicates a problem occurred with the session. Exceptions raised in a callback can be handled explicitly by specifying an exception_callback.
The callbacks are executed in the order they are added.
Info
It is recommended that callback functions be non-blocking to ensure the networking event loop remains running.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
record_callback | Callable[[DBNRecord], None] | required | A callback to dispatch records to. The callback must take a single record argument. If no exception callback is provided, any exceptions encountered will be logged and raised as warnings for visibility. |
exception_callback | Callable[[Exception], None] | optional | A callback to dispatch exceptions to that are raised when executing record_callback. The callback must take a single exception argument. |
API method
1 2 3 4
Live.add_callback(
record_callback: Callable[[DBNRecord], None],
exception_callback: Callable[[Exception], None] | None = None,
) -> None Example usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import databento as db
# Create a callback to handle DBN records
def user_callback(record: db.DBNRecord) -> None:
print(f"callback run for {record}")
# Create a callback to handle exceptions from `user_callback`
def error_handler(exception: Exception) -> None:
print(f"an error occurred {exception}")
# Create a live client
client = db.Live(key="$YOUR_API_KEY")
# Subscribe to the trades schema for all ES futures
client.subscribe(
dataset="GLBX.MDP3",
schema="trades",
stype_in="parent",
symbols="ES.FUT",
)
# Add the user_callback to the client
client.add_callback(
record_callback=user_callback,
exception_callback=error_handler, # optional error handler
)