Skip to content

Commit f595856

Browse files
authored
docs: pub docs for next_status_updates() (#765)
1 parent 9e7c7bf commit f595856

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

docs/docs/core/flow_methods.mdx

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,27 @@ A `FlowLiveUpdater` object supports the following methods:
241241

242242
* `start()`: Start the updater.
243243
CocoIndex will continuously capture changes from the source data and update the target data accordingly in background threads managed by the engine.
244+
244245
* `abort()`: Abort the updater.
246+
245247
* `wait()`: Wait for the updater to finish. It only unblocks in one of the following cases:
246248
* The updater was aborted.
247249
* A one time update is done, and live update is not enabled:
248250
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+
249261
* `update_stats()`: It returns the stats of the updater.
250262

263+
This snippets shows the lifecycle of a live updater:
264+
251265
```python
252266
my_updater = cocoindex.FlowLiveUpdater(demo_flow)
253267
# Start the updater.
@@ -256,14 +270,37 @@ my_updater.start()
256270
# Perform your own logic (e.g. a query loop).
257271
...
258272

259-
# Print the update stats.
260-
print(my_updater.update_stats())
261-
# Abort the updater.
262-
my_updater.abort()
273+
...
263274
# Wait for the updater to finish.
264275
my_updater.wait()
276+
# Print the update stats.
277+
print(my_updater.update_stats())
265278
```
266279

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+
while True:
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+
if not 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+
267304
Python SDK also allows you to use the updater as a context manager.
268305
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.
269306
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):
272309
with cocoindex.FlowLiveUpdater(demo_flow) as my_updater:
273310
# Perform your own logic (e.g. a query loop).
274311
...
275-
print(my_updater.update_stats())
276312
```
277313

278314
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
287323
# Perform your own logic (e.g. a query loop).
288324
...
289325

290-
# Print the update stats.
291-
print(my_updater.update_stats())
292-
# Abort the updater.
293-
my_updater.abort()
294326
# Wait for the updater to finish.
295327
await my_updater.wait_async()
328+
# Print the update stats.
329+
print(my_updater.update_stats())
330+
```
331+
332+
* `next_status_updates_async()`, e.g.
333+
334+
```python
335+
while True:
336+
updates = await my_updater.next_status_updates_async()
337+
338+
...
296339
```
340+
297341
* Async context manager, e.g.
298342

299343
```python
300344
async with cocoindex.FlowLiveUpdater(demo_flow) as my_updater:
301345
# Perform your own logic (e.g. a query loop).
302346
...
303-
print(my_updater.update_stats())
304347
```
305348

306349
</TabItem>

python/cocoindex/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .flow import FlowBuilder, DataScope, DataSlice, Flow, transform_flow
1111
from .flow import flow_def
1212
from .flow import EvaluateAndDumpOptions, GeneratedField
13-
from .flow import FlowLiveUpdater, FlowLiveUpdaterOptions
13+
from .flow import FlowLiveUpdater, FlowLiveUpdaterOptions, FlowUpdaterStatusUpdates
1414
from .flow import add_flow_def, remove_flow
1515
from .flow import update_all_flows_async, setup_all_flows, drop_all_flows
1616
from .lib import init, start_server, stop
@@ -54,6 +54,7 @@
5454
"GeneratedField",
5555
"FlowLiveUpdater",
5656
"FlowLiveUpdaterOptions",
57+
"FlowUpdaterStatusUpdates",
5758
"add_flow_def",
5859
"remove_flow",
5960
"update_all_flows_async",

0 commit comments

Comments
 (0)