Skip to content

Commit f24a5d3

Browse files
committed
sd
1 parent 4e66ccd commit f24a5d3

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

py-gxhash/src/lib.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use pyo3::exceptions::PyRuntimeError;
12
use pyo3::prelude::*;
3+
use pyo3_async_runtimes::tokio::future_into_py;
4+
use tokio::task::spawn_blocking;
25

36
#[pyfunction]
47
fn gxhash32(input_bytes: &[u8], seed: i64) -> PyResult<u32> {
@@ -14,12 +17,12 @@ fn gxhash32_nogil(py: Python, input_bytes: &[u8], seed: i64) -> PyResult<u32> {
1417
fn gxhash32_async<'p>(py: Python<'p>, input_bytes: &'p [u8], seed: i64) -> PyResult<Bound<'p, PyAny>> {
1518
let input_bytes_clone = input_bytes.to_vec();
1619

17-
pyo3_async_runtimes::tokio::future_into_py(py, async move {
18-
let result = tokio::task::spawn_blocking(move || gxhash::gxhash32(&input_bytes_clone, seed)).await;
20+
future_into_py(py, async move {
21+
let result = Python::with_gil(|py| py.allow_threads(|| spawn_blocking(move || gxhash::gxhash32(&input_bytes_clone, seed)))).await;
1922

2023
match result {
2124
Ok(result) => Ok(result),
22-
Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!("Task failed: {:?}", e))),
25+
Err(e) => Err(PyRuntimeError::new_err(format!("Task failed: {:?}", e))),
2326
}
2427
})
2528
}
@@ -38,12 +41,12 @@ fn gxhash64_nogil(py: Python, input_bytes: &[u8], seed: i64) -> PyResult<u64> {
3841
fn gxhash64_async<'p>(py: Python<'p>, input_bytes: &'p [u8], seed: i64) -> PyResult<Bound<'p, PyAny>> {
3942
let input_bytes_clone = input_bytes.to_vec();
4043

41-
pyo3_async_runtimes::tokio::future_into_py(py, async move {
42-
let result = tokio::task::spawn_blocking(move || gxhash::gxhash64(&input_bytes_clone, seed)).await;
44+
future_into_py(py, async move {
45+
let result = spawn_blocking(move || gxhash::gxhash64(&input_bytes_clone, seed)).await;
4346

4447
match result {
4548
Ok(result) => Ok(result),
46-
Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!("Task failed: {:?}", e))),
49+
Err(e) => Err(PyRuntimeError::new_err(format!("Task failed: {:?}", e))),
4750
}
4851
})
4952
}
@@ -62,30 +65,16 @@ fn gxhash128_nogil(py: Python, input_bytes: &[u8], seed: i64) -> PyResult<u128>
6265
fn gxhash128_async<'p>(py: Python<'p>, input_bytes: &'p [u8], seed: i64) -> PyResult<Bound<'p, PyAny>> {
6366
let input_bytes_clone = input_bytes.to_vec();
6467

65-
pyo3_async_runtimes::tokio::future_into_py(py, async move {
66-
let result = tokio::task::spawn_blocking(move || gxhash::gxhash128(&input_bytes_clone, seed)).await;
68+
future_into_py(py, async move {
69+
let result = spawn_blocking(move || gxhash::gxhash128(&input_bytes_clone, seed)).await;
6770

6871
match result {
6972
Ok(result) => Ok(result),
70-
Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!("Task failed: {:?}", e))),
73+
Err(e) => Err(PyRuntimeError::new_err(format!("Task failed: {:?}", e))),
7174
}
7275
})
7376
}
7477

75-
#[pyfunction]
76-
fn test_async(py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
77-
pyo3_async_runtimes::tokio::future_into_py(py, async move {
78-
let result = tokio::task::spawn_blocking(move || {
79-
std::thread::sleep(std::time::Duration::from_secs(1));
80-
42
81-
})
82-
.await
83-
.unwrap();
84-
85-
Ok(result)
86-
})
87-
}
88-
8978
#[pymodule(name = "gxhash")]
9079
fn pygxhash(m: &Bound<'_, PyModule>) -> PyResult<()> {
9180
m.add_function(wrap_pyfunction!(gxhash32, m)?)?;
@@ -97,6 +86,5 @@ fn pygxhash(m: &Bound<'_, PyModule>) -> PyResult<()> {
9786
m.add_function(wrap_pyfunction!(gxhash128, m)?)?;
9887
m.add_function(wrap_pyfunction!(gxhash128_nogil, m)?)?;
9988
m.add_function(wrap_pyfunction!(gxhash128_async, m)?)?;
100-
m.add_function(wrap_pyfunction!(test_async, m)?)?;
10189
Ok(())
10290
}

0 commit comments

Comments
 (0)