Live.add_reconnect_callback
Add a reconnect callback to the live client. This callback must take two arguments. When a reconnection policy is set on the Live client, this callback will be executed when a reconnection occurs with two arguments:
- The last received record's
ts_eventtimestamp, orstarttimestamp from the sessionMetadataif no records were received yet. - The
starttimestamp from the reconnected session'sMetadata.
These values can be used to record any gaps in the received data due to a disconnection. 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 |
|---|---|---|---|
reconnect_callback | Callable[[pd.Timestamp, pd.Timestamp], None] | required | A callback for handling reconnections. The callback must take two arguments. |
exception_callback | Callable[[Exception], None] | optional | A callback to dispatch exceptions to that are raised when executing reconnect_callback. The callback must take a single exception argument. |
API method
1 2 3 4
Live.add_reconnect_callback(
reconnect_callback: Callable[[pd.Timestamp, pd.Timestamp], 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 29 30 31
import databento as db
# Create a callback to handle reconnections
def reconnect_callback(start, end) -> None:
print(f"reconnection gap from {start} to {end}")
# Create a callback to handle exceptions from `reconnect_callback`
def error_handler(exception: Exception) -> None:
print(f"an error occurred {exception}")
# Create a live client with a reconnect policy
client = db.Live(
key="$YOUR_API_KEY",
reconnect_policy="reconnect",
)
# Subscribe to the trades schema for all ES futures
client.subscribe(
dataset="GLBX.MDP3",
schema="trades",
stype_in="parent",
symbols="ES.FUT",
)
# Add the reconnect callback to the client
client.add_reconnect_callback(
reconnect_callback=reconnect_callback,
exception_callback=error_handler, # optional error handler
)