@@ -65,7 +65,7 @@ use pyo3::prelude::*;
65
65
#[pyo3_asyncio:: async_std:: main]
66
66
async fn main () -> PyResult <()> {
67
67
let fut = Python :: with_gil (| py | {
68
- let asyncio = py . import (" asyncio" )? ;
68
+ let asyncio = py . import_bound (" asyncio" )? ;
69
69
// convert asyncio.sleep into a Rust Future
70
70
pyo3_asyncio :: async_std :: into_future (asyncio . call_method1 (" sleep" , (1. into_py (py ),))? )
71
71
})? ;
@@ -95,7 +95,7 @@ use pyo3::prelude::*;
95
95
#[pyo3_asyncio:: tokio:: main]
96
96
async fn main () -> PyResult <()> {
97
97
let fut = Python :: with_gil (| py | {
98
- let asyncio = py . import (" asyncio" )? ;
98
+ let asyncio = py . import_bound (" asyncio" )? ;
99
99
// convert asyncio.sleep into a Rust Future
100
100
pyo3_asyncio :: tokio :: into_future (asyncio . call_method1 (" sleep" , (1. into_py (py ),))? )
101
101
})? ;
@@ -149,7 +149,7 @@ Export an async function that makes use of `async-std`:
149
149
use pyo3 :: {prelude :: * , wrap_pyfunction};
150
150
151
151
#[pyfunction]
152
- fn rust_sleep (py : Python ) -> PyResult <& PyAny > {
152
+ fn rust_sleep (py : Python ) -> PyResult <Bound < PyAny > > {
153
153
pyo3_asyncio :: async_std :: future_into_py (py , async {
154
154
async_std :: task :: sleep (std :: time :: Duration :: from_secs (1 )). await ;
155
155
Ok (())
@@ -173,7 +173,7 @@ If you want to use `tokio` instead, here's what your module should look like:
173
173
use pyo3 :: {prelude :: * , wrap_pyfunction};
174
174
175
175
#[pyfunction]
176
- fn rust_sleep (py : Python ) -> PyResult <& PyAny > {
176
+ fn rust_sleep (py : Python ) -> PyResult <Bound < PyAny > > {
177
177
pyo3_asyncio :: tokio :: future_into_py (py , async {
178
178
tokio :: time :: sleep (std :: time :: Duration :: from_secs (1 )). await ;
179
179
Ok (())
@@ -237,7 +237,7 @@ use pyo3::prelude::*;
237
237
async fn main () -> PyResult <()> {
238
238
let future = Python :: with_gil (| py | -> PyResult <_ > {
239
239
// import the module containing the py_sleep function
240
- let example = py . import (" example" )? ;
240
+ let example = py . import_bound (" example" )? ;
241
241
242
242
// calling the py_sleep method like a normal function
243
243
// returns a coroutine
@@ -289,7 +289,7 @@ async fn rust_sleep() {
289
289
}
290
290
291
291
#[pyfunction]
292
- fn call_rust_sleep (py : Python ) -> PyResult <& PyAny > {
292
+ fn call_rust_sleep (py : Python ) -> PyResult <Bound < PyAny > > {
293
293
pyo3_asyncio :: async_std :: future_into_py (py , async move {
294
294
rust_sleep (). await ;
295
295
Ok (())
@@ -356,7 +356,7 @@ async fn main() -> PyResult<()> {
356
356
// PyO3 is initialized - Ready to go
357
357
358
358
let fut = Python :: with_gil (| py | -> PyResult <_ > {
359
- let asyncio = py . import (" asyncio" )? ;
359
+ let asyncio = py . import_bound (" asyncio" )? ;
360
360
361
361
// convert asyncio.sleep into a Rust Future
362
362
pyo3_asyncio :: async_std :: into_future (
@@ -443,7 +443,7 @@ tokio = "1.9"
443
443
use pyo3 :: {prelude :: * , wrap_pyfunction};
444
444
445
445
#[pyfunction]
446
- fn rust_sleep (py : Python ) -> PyResult <& PyAny > {
446
+ fn rust_sleep (py : Python ) -> PyResult <Bound < PyAny > > {
447
447
pyo3_asyncio :: tokio :: future_into_py (py , async {
448
448
tokio :: time :: sleep (std :: time :: Duration :: from_secs (1 )). await ;
449
449
Ok (())
@@ -504,7 +504,7 @@ fn main() -> PyResult<()> {
504
504
pyo3 :: prepare_freethreaded_python ();
505
505
506
506
Python :: with_gil (| py | {
507
- let uvloop = py . import (" uvloop" )? ;
507
+ let uvloop = py . import_bound (" uvloop" )? ;
508
508
uvloop . call_method0 (" install" )? ;
509
509
510
510
// store a reference for the assertion
@@ -514,11 +514,11 @@ fn main() -> PyResult<()> {
514
514
// verify that we are on a uvloop.Loop
515
515
Python :: with_gil (| py | -> PyResult <()> {
516
516
assert! (uvloop
517
- . as_ref (py )
517
+ . bind (py )
518
518
. getattr (" Loop" )?
519
519
. downcast :: <PyType >()
520
520
. unwrap ()
521
- . is_instance (pyo3_asyncio :: async_std :: get_current_loop (py )? )? );
521
+ . is_instance (& pyo3_asyncio :: async_std :: get_current_loop (py )? )? );
522
522
Ok (())
523
523
})? ;
524
524
@@ -601,24 +601,24 @@ To make things a bit easier, I decided to keep most of the old API alongside the
601
601
pyo3 :: prepare_freethreaded_python ();
602
602
603
603
Python :: with_gil (| py | {
604
- let asyncio = py . import (" asyncio" )? ;
604
+ let asyncio = py . import_bound (" asyncio" )? ;
605
605
606
606
let event_loop = asyncio . call_method0 (" new_event_loop" )? ;
607
- asyncio . call_method1 (" set_event_loop" , (event_loop ,))? ;
607
+ asyncio . call_method1 (" set_event_loop" , (& event_loop ,))? ;
608
608
609
- let event_loop_hdl = PyObject :: from (event_loop );
609
+ let event_loop_hdl = PyObject :: from (event_loop . clone () );
610
610
611
611
pyo3_asyncio :: tokio :: get_runtime (). spawn (async move {
612
612
tokio :: time :: sleep (std :: time :: Duration :: from_secs (1 )). await ;
613
613
614
614
// Stop the event loop manually
615
615
Python :: with_gil (| py | {
616
616
event_loop_hdl
617
- . as_ref (py )
617
+ . bind (py )
618
618
. call_method1 (
619
619
" call_soon_threadsafe" ,
620
620
(event_loop_hdl
621
- . as_ref (py )
621
+ . bind (py )
622
622
. getattr (" stop" )
623
623
. unwrap (),),
624
624
)
0 commit comments