Generate embedding for a single text CRITICAL: This method releases the GIL during execution, enabling true parallelism from Python threads. Multiple threads can call this method simultaneously.
(&self, py: Python<'_>, text: String)
| 31 | /// CRITICAL: This method releases the GIL during execution, enabling true parallelism |
| 32 | /// from Python threads. Multiple threads can call this method simultaneously. |
| 33 | fn embed(&self, py: Python<'_>, text: String) -> PyResult<Vec<f32>> { |
| 34 | // Validate input |
| 35 | if text.is_empty() { |
| 36 | return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>( |
| 37 | "Text input cannot be empty", |
| 38 | )); |
| 39 | } |
| 40 | |
| 41 | let service = Arc::clone(&self.service); |
| 42 | let rt = get_runtime(); |
| 43 | |
| 44 | // CRITICAL FIX: Release GIL during async execution |
| 45 | // This enables multiple Python threads to execute embed() in parallel |
| 46 | py.allow_threads(|| { |
| 47 | rt.block_on(async move { |
| 48 | let response = service |
| 49 | .embed_text(&text) |
| 50 | .await |
| 51 | .map_err(to_py_runtime_error)?; |
| 52 | Ok(response) |
| 53 | }) |
| 54 | }) |
| 55 | } |
| 56 | |
| 57 | /// Generate embeddings for multiple texts |
| 58 | /// |