Skip to content

Commit 0d22d39

Browse files
authored
docs: add docs for lifecycle events (#360)
1 parent bb6e8d2 commit 0d22d39

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,14 @@ client = UnleashClient(
243243

244244
```
245245

246-
### Impression data
246+
### Events and impression data
247247

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.
249249

250250
**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.
251251

252+
#### Impression events
253+
252254
To use impression data:
253255
- Enable impression data on your feature flags in the Unleash UI.
254256
- Provide an event_callback function when you initialize the client.
@@ -282,6 +284,41 @@ client.is_enabled("testFlag")
282284

283285
Impression callbacks run in-process — keep them fast to avoid blocking your app.
284286

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+
def receive_data(sender, **kw):
302+
if kw["data"].event_type == UnleashEventType.READY:
303+
print("SDK is ready: toggles loaded from Unleash or backup")
304+
elif kw["data"].event_type == UnleashEventType.FETCHED:
305+
# Only FETCHED events have a 'features' property
306+
print("Fetched new feature flags:", kw["data"].features)
307+
308+
def example_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+
285322
### Custom cache
286323

287324
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

Comments
 (0)