1
- use std:: { future:: Future , thread, time:: Duration } ;
1
+ mod common;
2
+
3
+ use std:: { future:: Future , time:: Duration } ;
2
4
3
5
use async_std:: task;
4
6
use pyo3:: { prelude:: * , wrap_pyfunction} ;
@@ -18,16 +20,6 @@ fn sleep_for(py: Python, secs: &PyAny) -> PyResult<PyObject> {
18
20
} )
19
21
}
20
22
21
- const TEST_MOD : & ' static str = r#"
22
- import asyncio
23
-
24
- async def py_sleep(duration):
25
- await asyncio.sleep(duration)
26
-
27
- async def sleep_for_1s(sleep_for):
28
- await sleep_for(1)
29
- "# ;
30
-
31
23
fn test_into_coroutine (
32
24
py : Python ,
33
25
) -> PyResult < impl Future < Output = PyResult < ( ) > > + Send + ' static > {
@@ -37,8 +29,13 @@ fn test_into_coroutine(
37
29
. as_ref ( py)
38
30
. add_wrapped ( wrap_pyfunction ! ( sleep_for) ) ?;
39
31
40
- let test_mod: PyObject =
41
- PyModule :: from_code ( py, TEST_MOD , "test_rust_coroutine/test_mod.py" , "test_mod" ) ?. into ( ) ;
32
+ let test_mod: PyObject = PyModule :: from_code (
33
+ py,
34
+ common:: TEST_MOD ,
35
+ "test_rust_coroutine/test_mod.py" ,
36
+ "test_mod" ,
37
+ ) ?
38
+ . into ( ) ;
42
39
43
40
Ok ( async move {
44
41
Python :: with_gil ( |py| {
@@ -54,24 +51,6 @@ fn test_into_coroutine(
54
51
} )
55
52
}
56
53
57
- fn test_into_future ( py : Python ) -> PyResult < impl Future < Output = PyResult < ( ) > > + Send + ' static > {
58
- let test_mod: PyObject =
59
- PyModule :: from_code ( py, TEST_MOD , "test_rust_coroutine/test_mod.py" , "test_mod" ) ?. into ( ) ;
60
-
61
- Ok ( async move {
62
- Python :: with_gil ( |py| {
63
- pyo3_asyncio:: into_future (
64
- py,
65
- test_mod
66
- . call_method1 ( py, "py_sleep" , ( 1 . into_py ( py) , ) ) ?
67
- . as_ref ( py) ,
68
- )
69
- } ) ?
70
- . await ?;
71
- Ok ( ( ) )
72
- } )
73
- }
74
-
75
54
fn test_async_sleep < ' p > (
76
55
py : Python < ' p > ,
77
56
) -> PyResult < impl Future < Output = PyResult < ( ) > > + Send + ' static > {
@@ -89,10 +68,6 @@ fn test_async_sleep<'p>(
89
68
} )
90
69
}
91
70
92
- fn test_blocking_sleep ( ) {
93
- thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
94
- }
95
-
96
71
fn main ( ) {
97
72
test_main (
98
73
"PyO3 Asyncio Test Suite" ,
@@ -108,7 +83,7 @@ fn main() {
108
83
} ) ,
109
84
) ,
110
85
new_sync_test( "test_blocking_sleep" . into( ) , || {
111
- test_blocking_sleep( ) ;
86
+ common :: test_blocking_sleep( ) ;
112
87
Ok ( ( ) )
113
88
} ) ,
114
89
Test :: new_async(
@@ -124,7 +99,7 @@ fn main() {
124
99
Test :: new_async(
125
100
"test_into_future" . into( ) ,
126
101
Python :: with_gil( |py| {
127
- test_into_future( py)
102
+ common :: test_into_future( py)
128
103
. map_err( |e| {
129
104
e. print_and_set_sys_last_vars( py) ;
130
105
} )
0 commit comments