Skip to content

Commit df8c96d

Browse files
authored
feat(api): introduce Flow.close() to replace cocoindex.remove_flow() (#829)
1 parent b87816c commit df8c96d

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

docs/docs/core/flow_def.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ demo_flow = cocoindex.add_flow_def("DemoFlow", demo_flow_def)
4646
In both cases, `demo_flow` will be an object with `cocoindex.Flow` class type.
4747
See [Flow Running](/docs/core/flow_methods) for more details on it.
4848

49-
Sometimes you no longer want to keep states of the flow in memory. We provide a `cocoindex.remove_flow()` method for this purpose:
49+
Sometimes you no longer want to keep states of the flow in memory. We provide a `close()` method for this purpose:
5050

5151
```python
52-
cocoindex.remove_flow(demo_flow)
52+
demo_flow.close()
5353
```
5454

5555
After it's called, `demo_flow` becomes an invalid object, and you should not call any methods of it.

docs/docs/core/flow_methods.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ It creates a `demo_flow` object in `cocoindex.Flow` type.
3939
For a flow, its persistent backends need to be ready before it can run, including:
4040

4141
* [Internal storage](/docs/core/basics#internal-storage) for CocoIndex.
42-
* Backend entities for targets exported by the flow, e.g. a table (in relational databases), a collection (in some vector databases), etc.
42+
* Backend resources for targets exported by the flow, e.g. a table (in relational databases), a collection (in some vector databases), etc.
4343

4444
The desired state of the backends for a flow is derived based on the flow definition itself.
4545
CocoIndex supports two types of actions to manage the persistent backends automatically:
@@ -104,7 +104,7 @@ cocoindex.drop_all_flows(report_to_stdout=True)
104104

105105
After dropping the flow, the in-memory `cocoindex.Flow` instance is still valid, and you can call setup methods on it again.
106106

107-
If you want to remove the flow from the current process, you can call `cocoindex.remove_flow(demo_flow)` to do so (see [related doc](/docs/core/flow_def#entry-point)).
107+
If you want to remove the flow from the current process, you can call `demo_flow.close()` to do so (see [related doc](/docs/core/flow_def#entry-point)).
108108

109109
:::
110110

python/cocoindex/flow.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ def drop(self, report_to_stdout: bool = False) -> None:
798798
The current instance is still valid after it's called.
799799
For example, you can still call `setup()` after it, to setup the persistent backends again.
800800
801-
Call `cocoindex.remove_flow()` if you want to remove the flow from the current process.
801+
Call `close()` if you want to remove the flow from the current process.
802802
"""
803803
execution_context.run(self.drop_async(report_to_stdout=report_to_stdout))
804804

@@ -810,6 +810,18 @@ async def drop_async(self, report_to_stdout: bool = False) -> None:
810810
report_to_stdout=report_to_stdout
811811
)
812812

813+
def close(self) -> None:
814+
"""
815+
Close the flow. It will remove the flow from the current process to free up resources.
816+
After it's called, methods of the flow should no longer be called.
817+
818+
This will NOT touch the persistent backends of the flow.
819+
"""
820+
_engine.remove_flow_context(self.full_name)
821+
self._lazy_engine_flow = None
822+
with _flows_lock:
823+
del _flows[self.name]
824+
813825

814826
def _create_lazy_flow(
815827
name: str | None, fl_def: Callable[[FlowBuilder, DataScope], None]
@@ -855,15 +867,9 @@ def add_flow_def(name: str, fl_def: Callable[[FlowBuilder, DataScope], None]) ->
855867

856868
def remove_flow(fl: Flow) -> None:
857869
"""
858-
Remove a flow from the current process to free up resources.
859-
After it's called, methods of the flow should no longer be called.
860-
861-
This will NOT touch the persistent backends of the flow.
870+
DEPRECATED: Use `Flow.close()` instead.
862871
"""
863-
_engine.remove_flow_context(fl.full_name)
864-
fl._lazy_engine_flow = None # pylint: disable=protected-access
865-
with _flows_lock:
866-
del _flows[fl.name]
872+
fl.close()
867873

868874

869875
def flow_def(

0 commit comments

Comments
 (0)