Skip to content

Commit a374e91

Browse files
committed
Support eneabling cache in Python SDK.
1 parent 6b6788a commit a374e91

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

python/cocoindex/op.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ def __call__(self, spec_json: str, *args, **kwargs):
6262

6363
_gpu_dispatch_lock = Lock()
6464

65-
def executor_class(gpu: bool = False) -> Callable[[type], type]:
65+
def executor_class(gpu: bool = False, cache: bool = False, behavior_version: int | None = None) -> Callable[[type], type]:
6666
"""
6767
Decorate a class to provide an executor for an op.
6868
6969
Args:
7070
gpu: Whether the executor will be executed on GPU.
71+
cache: Whether the executor will be cached.
72+
behavior_version: The behavior version of the executor. Cache will be invalidated if it changes. Must be provided if `cache` is True.
7173
"""
7274

7375
def _inner(cls: type[Executor]) -> type:
@@ -87,7 +89,15 @@ def _inner(cls: type[Executor]) -> type:
8789
expected_return = sig.return_annotation
8890

8991
cls_type: type = cls
90-
class _WrappedClass(cls_type):
92+
93+
class _Fallback:
94+
def enable_cache(self):
95+
return cache
96+
97+
def behavior_version(self):
98+
return behavior_version
99+
100+
class _WrappedClass(cls_type, _Fallback):
91101
def __init__(self, spec):
92102
super().__init__()
93103
self.spec = spec

src/ops/py_factory.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ struct PyFunctionExecutor {
149149
num_positional_args: usize,
150150
kw_args_names: Vec<Py<PyString>>,
151151
result_type: schema::EnrichedValueType,
152+
153+
enable_cache: bool,
154+
behavior_version: Option<u32>,
152155
}
153156

154157
#[async_trait]
@@ -193,6 +196,14 @@ impl SimpleFunctionExecutor for Arc<PyFunctionExecutor> {
193196
})
194197
.await
195198
}
199+
200+
fn enable_cache(&self) -> bool {
201+
self.enable_cache
202+
}
203+
204+
fn behavior_version(&self) -> Option<u32> {
205+
self.behavior_version
206+
}
196207
}
197208

198209
pub(crate) struct PyFunctionFactory {
@@ -251,15 +262,27 @@ impl SimpleFunctionFactory for PyFunctionFactory {
251262

252263
let executor_fut = {
253264
let result_type = result_type.clone();
254-
async move {
255-
Python::with_gil(|py| executor.call_method(py, "prepare", (), None))?;
265+
unblock(move || {
266+
let (enable_cache, behavior_version) =
267+
Python::with_gil(|py| -> anyhow::Result<_> {
268+
executor.call_method(py, "prepare", (), None)?;
269+
let enable_cache = executor.call_method(py, "enable_cache", (), None)?;
270+
let behavior_version =
271+
executor.call_method(py, "behavior_version", (), None)?;
272+
Ok((
273+
enable_cache.extract::<bool>(py)?,
274+
behavior_version.extract::<Option<u32>>(py)?,
275+
))
276+
})?;
256277
Ok(Box::new(Arc::new(PyFunctionExecutor {
257278
py_function_executor: executor,
258279
num_positional_args,
259280
kw_args_names,
260281
result_type,
282+
enable_cache,
283+
behavior_version,
261284
})) as Box<dyn SimpleFunctionExecutor>)
262-
}
285+
})
263286
};
264287

265288
Ok((result_type, executor_fut.boxed()))

0 commit comments

Comments
 (0)