🧵 p3: run_python_parallel for free-threaded dispatch (ADR-087)
swadhinbiswas committed 2 months ago
8d3c5dc
1 parent 3e6fe90
commit 8d3c5dc
1 file changed
+16 -0
Showing 1 changed file+16-0
▼crates/justapi-py/src/gil_pool.rs
+16-0
| @@ -276,3 +276,19 @@ pub fn mode() -> Option<RuntimeMode> { | ||
276 | 276 | let guard = POOL.lock().unwrap_or_else(|e| e.into_inner()); |
277 | 277 | guard.as_ref().map(|p| p.mode) |
278 | 278 | } |
279 | + | |
280 | +/// Run a Python closure on the tokio blocking pool (PARALLEL). | |
281 | +/// | |
282 | +/// Correct ONLY on free-threaded CPython (Py_GIL_DISABLED): there is no GIL | |
283 | +/// to thrash, so `@native_async` handlers dispatch in true parallel | |
284 | +/// (available_parallelism workers). On GIL-locked builds this thrashes and | |
285 | +/// must not be used (measured regression, ADR-087). | |
286 | +pub async fn run_python_parallel<F, T>(f: F) -> anyhow::Result<T> | |
287 | +where | |
288 | + F: FnOnce(Python<'_>) -> T + Send + 'static, | |
289 | + T: Send + 'static, | |
290 | +{ | |
291 | + tokio::task::spawn_blocking(move || Python::attach(f)) | |
292 | + .await | |
293 | + .map_err(|e| anyhow::anyhow!("python blocking task error: {}", e)) | |
294 | +} | |
279 | 295 | |