1
+ use pyo3:: exceptions:: PyRuntimeError ;
1
2
use pyo3:: prelude:: * ;
3
+ use pyo3_async_runtimes:: tokio:: future_into_py;
4
+ use tokio:: task:: spawn_blocking;
2
5
3
6
#[ pyfunction]
4
7
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> {
14
17
fn gxhash32_async < ' p > ( py : Python < ' p > , input_bytes : & ' p [ u8 ] , seed : i64 ) -> PyResult < Bound < ' p , PyAny > > {
15
18
let input_bytes_clone = input_bytes. to_vec ( ) ;
16
19
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 ;
19
22
20
23
match result {
21
24
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) ) ) ,
23
26
}
24
27
} )
25
28
}
@@ -38,12 +41,12 @@ fn gxhash64_nogil(py: Python, input_bytes: &[u8], seed: i64) -> PyResult<u64> {
38
41
fn gxhash64_async < ' p > ( py : Python < ' p > , input_bytes : & ' p [ u8 ] , seed : i64 ) -> PyResult < Bound < ' p , PyAny > > {
39
42
let input_bytes_clone = input_bytes. to_vec ( ) ;
40
43
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 ;
43
46
44
47
match result {
45
48
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) ) ) ,
47
50
}
48
51
} )
49
52
}
@@ -62,30 +65,16 @@ fn gxhash128_nogil(py: Python, input_bytes: &[u8], seed: i64) -> PyResult<u128>
62
65
fn gxhash128_async < ' p > ( py : Python < ' p > , input_bytes : & ' p [ u8 ] , seed : i64 ) -> PyResult < Bound < ' p , PyAny > > {
63
66
let input_bytes_clone = input_bytes. to_vec ( ) ;
64
67
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 ;
67
70
68
71
match result {
69
72
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) ) ) ,
71
74
}
72
75
} )
73
76
}
74
77
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
-
89
78
#[ pymodule( name = "gxhash" ) ]
90
79
fn pygxhash ( m : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
91
80
m. add_function ( wrap_pyfunction ! ( gxhash32, m) ?) ?;
@@ -97,6 +86,5 @@ fn pygxhash(m: &Bound<'_, PyModule>) -> PyResult<()> {
97
86
m. add_function ( wrap_pyfunction ! ( gxhash128, m) ?) ?;
98
87
m. add_function ( wrap_pyfunction ! ( gxhash128_nogil, m) ?) ?;
99
88
m. add_function ( wrap_pyfunction ! ( gxhash128_async, m) ?) ?;
100
- m. add_function ( wrap_pyfunction ! ( test_async, m) ?) ?;
101
89
Ok ( ( ) )
102
90
}
0 commit comments