|
1 | 1 | use std::{collections::BTreeMap, sync::Arc}; |
2 | 2 |
|
3 | 3 | use axum::async_trait; |
4 | | -use blocking::unblock; |
5 | 4 | use futures::FutureExt; |
6 | 5 | use pyo3::{ |
7 | 6 | exceptions::PyException, |
@@ -219,7 +218,7 @@ struct PyFunctionExecutor { |
219 | 218 | impl SimpleFunctionExecutor for Arc<PyFunctionExecutor> { |
220 | 219 | async fn evaluate(&self, input: Vec<value::Value>) -> Result<value::Value> { |
221 | 220 | let self = self.clone(); |
222 | | - unblock(move || { |
| 221 | + let result = tokio::task::spawn_blocking(move || { |
223 | 222 | Python::with_gil(|py| -> Result<_> { |
224 | 223 | let mut args = Vec::with_capacity(self.num_positional_args); |
225 | 224 | for v in input[0..self.num_positional_args].iter() { |
@@ -255,7 +254,8 @@ impl SimpleFunctionExecutor for Arc<PyFunctionExecutor> { |
255 | 254 | )?) |
256 | 255 | }) |
257 | 256 | }) |
258 | | - .await |
| 257 | + .await??; |
| 258 | + Ok(result) |
259 | 259 | } |
260 | 260 |
|
261 | 261 | fn enable_cache(&self) -> bool { |
@@ -323,27 +323,31 @@ impl SimpleFunctionFactory for PyFunctionFactory { |
323 | 323 |
|
324 | 324 | let executor_fut = { |
325 | 325 | let result_type = result_type.clone(); |
326 | | - unblock(move || { |
327 | | - let (enable_cache, behavior_version) = |
328 | | - Python::with_gil(|py| -> anyhow::Result<_> { |
329 | | - executor.call_method(py, "prepare", (), None)?; |
330 | | - let enable_cache = executor |
331 | | - .call_method(py, "enable_cache", (), None)? |
332 | | - .extract::<bool>(py)?; |
333 | | - let behavior_version = executor |
334 | | - .call_method(py, "behavior_version", (), None)? |
335 | | - .extract::<Option<u32>>(py)?; |
336 | | - Ok((enable_cache, behavior_version)) |
337 | | - })?; |
338 | | - Ok(Box::new(Arc::new(PyFunctionExecutor { |
339 | | - py_function_executor: executor, |
340 | | - num_positional_args, |
341 | | - kw_args_names, |
342 | | - result_type, |
343 | | - enable_cache, |
344 | | - behavior_version, |
345 | | - })) as Box<dyn SimpleFunctionExecutor>) |
346 | | - }) |
| 326 | + async move { |
| 327 | + let executor = tokio::task::spawn_blocking(move || -> Result<_> { |
| 328 | + let (enable_cache, behavior_version) = |
| 329 | + Python::with_gil(|py| -> anyhow::Result<_> { |
| 330 | + executor.call_method(py, "prepare", (), None)?; |
| 331 | + let enable_cache = executor |
| 332 | + .call_method(py, "enable_cache", (), None)? |
| 333 | + .extract::<bool>(py)?; |
| 334 | + let behavior_version = executor |
| 335 | + .call_method(py, "behavior_version", (), None)? |
| 336 | + .extract::<Option<u32>>(py)?; |
| 337 | + Ok((enable_cache, behavior_version)) |
| 338 | + })?; |
| 339 | + Ok(Box::new(Arc::new(PyFunctionExecutor { |
| 340 | + py_function_executor: executor, |
| 341 | + num_positional_args, |
| 342 | + kw_args_names, |
| 343 | + result_type, |
| 344 | + enable_cache, |
| 345 | + behavior_version, |
| 346 | + })) as Box<dyn SimpleFunctionExecutor>) |
| 347 | + }) |
| 348 | + .await??; |
| 349 | + Ok(executor) |
| 350 | + } |
347 | 351 | }; |
348 | 352 |
|
349 | 353 | Ok((result_type, executor_fut.boxed())) |
|
0 commit comments