Skip to content

Commit 87e538e

Browse files
authored
docs(python): Document SDK connection config (#14043)
1 parent 3fd93e4 commit 87e538e

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

contents/docs/libraries/python/index.mdx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,50 @@ if settings.TEST:
227227
posthog.disabled = True
228228
```
229229

230+
## Connection configuration
231+
232+
The SDK uses HTTP connection pooling internally for better performance. These settings typically need not be changed, but in some environments, such as when running behind NAT gateways, pooled connections may be terminated non-gracefully, causing request failures.
233+
234+
You can configure connection behavior in several ways. The following settings should be called during initialization, before any API requests are made.
235+
236+
### Enable TCP keepalive
237+
238+
TCP keepalive probes help prevent idle connections from being dropped by network infrastructure. This is the recommended approach for most cases where idle connections are terminated.
239+
240+
```python
241+
import posthog
242+
243+
posthog.enable_keep_alive()
244+
```
245+
246+
This enables TCP keepalive with sensible defaults (60 second idle time, 60 second probe interval, 3 probes before timeout).
247+
248+
### Disable connection pooling
249+
250+
If you need each request to use a fresh connection, you can disable connection reuse entirely. This will incur additional overhead per request but may be desirable in some circumstances.
251+
252+
```python
253+
import posthog
254+
255+
posthog.disable_connection_reuse()
256+
```
257+
258+
### Custom HTTP socket options
259+
260+
For advanced use cases, you can configure arbitrary socket options on the underlying HTTP connection.
261+
262+
```python
263+
import socket
264+
import posthog
265+
266+
posthog.set_socket_options([
267+
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
268+
# Add additional socket options as needed
269+
])
270+
```
271+
272+
Pass `None` to `set_socket_options()` to reset to default behavior.
273+
230274
## Historical migrations
231275

232276
You can use the Python or Node SDK to run [historical migrations](/docs/migrate) of data into PostHog. To do so, set the `historical_migration` option to `true` when initializing the client.

0 commit comments

Comments
 (0)