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: docs/docs/core/flow_methods.mdx
+53-10Lines changed: 53 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -241,13 +241,27 @@ A `FlowLiveUpdater` object supports the following methods:
241
241
242
242
*`start()`: Start the updater.
243
243
CocoIndex will continuously capture changes from the source data and update the target data accordingly in background threads managed by the engine.
244
+
244
245
*`abort()`: Abort the updater.
246
+
245
247
*`wait()`: Wait for the updater to finish. It only unblocks in one of the following cases:
246
248
* The updater was aborted.
247
249
* A one time update is done, and live update is not enabled:
248
250
either `live_mode` is `False`, or all data sources have no change capture mechanisms enabled.
251
+
252
+
*`next_status_updates()`: Get the next status updates.
253
+
It blocks until there's a new status updates, including the processing finishes for a bunch of source updates, and live updater stops (aborted, or no more sources to process).
254
+
You can continuously call this method in a loop to get the latest status updates and react accordingly.
255
+
256
+
It returns a `cocoindex.FlowUpdaterStatusUpdates` object, with the following properties:
257
+
*`active_sources`: Names of sources that are still active, i.e. not stopped processing. If it's empty, it means the updater is stopped.
258
+
*`updated_sources`: Names of sources with updates since last time.
259
+
You can check this to see which sources have recent updates and get processed.
260
+
249
261
*`update_stats()`: It returns the stats of the updater.
250
262
263
+
This snippets shows the lifecycle of a live updater:
264
+
251
265
```python
252
266
my_updater = cocoindex.FlowLiveUpdater(demo_flow)
253
267
# Start the updater.
@@ -256,14 +270,37 @@ my_updater.start()
256
270
# Perform your own logic (e.g. a query loop).
257
271
...
258
272
259
-
# Print the update stats.
260
-
print(my_updater.update_stats())
261
-
# Abort the updater.
262
-
my_updater.abort()
273
+
...
263
274
# Wait for the updater to finish.
264
275
my_updater.wait()
276
+
# Print the update stats.
277
+
print(my_updater.update_stats())
265
278
```
266
279
280
+
Somewhere (in the same or other threads) you can also continuously call `next_status_updates()` to get the latest status updates and react accordingly, e.g.
281
+
282
+
```python
283
+
whileTrue:
284
+
updates = my_updater.next_status_updates()
285
+
286
+
for source in updates.updated_sources:
287
+
# Perform downstream operations on the target of the source.
288
+
run_your_downstream_operations_for(source)
289
+
290
+
# Break the loop if there's no more active sources.
291
+
ifnot updates.active_sources:
292
+
break
293
+
```
294
+
295
+
:::info
296
+
297
+
`next_status_updates()` automatically combines multiple status updates if more than one arrives between two calls,
298
+
e.g. your downstream operations may take more time, or you don't need to process too frequently (in which case you can explicitly sleep for a while).
299
+
300
+
So you don't need to worry about the status updates piling up.
301
+
302
+
:::
303
+
267
304
Python SDK also allows you to use the updater as a context manager.
268
305
It will automatically start the updater during the context entry, and abort and wait for the updater to finish automatically when the context is exited.
269
306
The following code is equivalent to the code above (if no early return happens):
@@ -272,7 +309,6 @@ The following code is equivalent to the code above (if no early return happens):
272
309
with cocoindex.FlowLiveUpdater(demo_flow) as my_updater:
273
310
# Perform your own logic (e.g. a query loop).
274
311
...
275
-
print(my_updater.update_stats())
276
312
```
277
313
278
314
CocoIndex also provides asynchronous versions of APIs for blocking operations, including:
@@ -287,20 +323,27 @@ CocoIndex also provides asynchronous versions of APIs for blocking operations, i
0 commit comments