Skip to content

Commit

Permalink
try without extract
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Nov 15, 2024
1 parent 37cb902 commit d661b7d
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions py-gxhash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ fn gxhash32_nogil(py: Python, input_bytes: &[u8], seed: i64) -> PyResult<u32> {
py.allow_threads(|| Ok(gxhash::gxhash32(input_bytes, seed)))
}

// #[pyfunction]
// fn test_async(py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
// pyo3_async_runtimes::tokio::future_into_py(py, async move {
// let result = tokio::task::spawn_blocking(move || {
// std::thread::sleep(std::time::Duration::from_secs(1));
// 42
// })
// .await
// .unwrap();

// Ok(result)
// })
// }

#[pyfunction]
fn gxhash32_async<'p>(py: Python<'p>, input_bytes: Bound<'p, PyAny>, seed: i64) -> PyResult<Bound<'p, PyAny>> {
let input_bytes = input_bytes.extract::<Vec<u8>>()?;
fn gxhash32_async<'a>(py: Python<'a>, input_bytes: &'a [u8], seed: i64) -> PyResult<Bound<'a, PyAny>> {
let input_bytes_clone = input_bytes.to_vec();

pyo3_async_runtimes::tokio::future_into_py(py, async move {
let result = pyo3_async_runtimes::tokio::get_runtime()
.spawn_blocking(move || gxhash::gxhash32(&input_bytes, seed))
.await
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Task failed: {:?}", e)))?;

let result = tokio::task::spawn_blocking(move || gxhash::gxhash32(&input_bytes_clone, seed)).await.unwrap();
Ok(result)
})
}
Expand Down

0 comments on commit d661b7d

Please # to comment.