You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-2Lines changed: 39 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -243,12 +243,14 @@ client = UnleashClient(
243
243
244
244
```
245
245
246
-
### Impression data
246
+
### Events and impression data
247
247
248
-
The Python SDK supports [impression data](https://docs.getunleash.io/reference/impression-data). This lets you capture an event for every feature toggle evaluation.
248
+
The Python SDK lets you tap into its behavior through [impression data](https://docs.getunleash.io/reference/impression-data) and lifecycle events.
249
249
250
250
**Note**: The SDK does not include a built-in event bus — you’ll need to provide your own. The example below shows how to use [Blinker](https://pypi.org/project/blinker/) to send signals.
251
251
252
+
#### Impression events
253
+
252
254
To use impression data:
253
255
- Enable impression data on your feature flags in the Unleash UI.
254
256
- Provide an event_callback function when you initialize the client.
Impression callbacks run in-process — keep them fast to avoid blocking your app.
284
286
287
+
#### Lifecycle events
288
+
289
+
The same event_callback also delivers lifecycle events:
290
+
- FETCHED: triggered when a new version of feature flags is pulled from the Unleash server. (Does not trigger on 304 Not Modified). The FETCHED event includes a features property containing all the feature flags returned by that fetch.
291
+
- READY: triggered once when the SDK first loads feature flags from the Unleash server or a local backup.
292
+
293
+
```python
294
+
from blinker import signal
295
+
from UnleashClient import UnleashClient
296
+
from UnleashClient.events import UnleashEvent, UnleashEventType
297
+
298
+
send_data = signal('send-data')
299
+
300
+
@send_data.connect
301
+
defreceive_data(sender, **kw):
302
+
if kw["data"].event_type == UnleashEventType.READY:
303
+
print("SDK is ready: toggles loaded from Unleash or backup")
print("Fetched new feature flags:", kw["data"].features)
307
+
308
+
defexample_callback(event: UnleashEvent):
309
+
send_data.send('anonymous', data=event)
310
+
311
+
client = UnleashClient(
312
+
url="https://YOUR-API-URL",
313
+
app_name="my-python-app",
314
+
custom_headers={'Authorization': '<API token>'},
315
+
event_callback=example_callback
316
+
)
317
+
client.initialize_client()
318
+
client.is_enabled("testFlag")
319
+
320
+
```
321
+
285
322
### Custom cache
286
323
287
324
By default, the Python SDK stores feature toggles in an on-disk cache using fcache. If you need a different storage backend, for example, Redis, memory-only, or a custom database, you can provide your own cache implementation.
0 commit comments