@@ -65,7 +65,7 @@ use pyo3::prelude::*;
6565#[pyo3_asyncio:: async_std:: main]
6666async fn main () -> PyResult <()> {
6767 let fut = Python :: with_gil (| py | {
68- let asyncio = py . import (" asyncio" )? ;
68+ let asyncio = py . import_bound (" asyncio" )? ;
6969 // convert asyncio.sleep into a Rust Future
7070 pyo3_asyncio :: async_std :: into_future (asyncio . call_method1 (" sleep" , (1. into_py (py ),))? )
7171 })? ;
@@ -95,7 +95,7 @@ use pyo3::prelude::*;
9595#[pyo3_asyncio:: tokio:: main]
9696async fn main () -> PyResult <()> {
9797 let fut = Python :: with_gil (| py | {
98- let asyncio = py . import (" asyncio" )? ;
98+ let asyncio = py . import_bound (" asyncio" )? ;
9999 // convert asyncio.sleep into a Rust Future
100100 pyo3_asyncio :: tokio :: into_future (asyncio . call_method1 (" sleep" , (1. into_py (py ),))? )
101101 })? ;
@@ -149,7 +149,7 @@ Export an async function that makes use of `async-std`:
149149use pyo3 :: {prelude :: * , wrap_pyfunction};
150150
151151#[pyfunction]
152- fn rust_sleep (py : Python ) -> PyResult <& PyAny > {
152+ fn rust_sleep (py : Python ) -> PyResult <Bound < PyAny > > {
153153 pyo3_asyncio :: async_std :: future_into_py (py , async {
154154 async_std :: task :: sleep (std :: time :: Duration :: from_secs (1 )). await ;
155155 Ok (())
@@ -173,7 +173,7 @@ If you want to use `tokio` instead, here's what your module should look like:
173173use pyo3 :: {prelude :: * , wrap_pyfunction};
174174
175175#[pyfunction]
176- fn rust_sleep (py : Python ) -> PyResult <& PyAny > {
176+ fn rust_sleep (py : Python ) -> PyResult <Bound < PyAny > > {
177177 pyo3_asyncio :: tokio :: future_into_py (py , async {
178178 tokio :: time :: sleep (std :: time :: Duration :: from_secs (1 )). await ;
179179 Ok (())
@@ -237,7 +237,7 @@ use pyo3::prelude::*;
237237async fn main () -> PyResult <()> {
238238 let future = Python :: with_gil (| py | -> PyResult <_ > {
239239 // import the module containing the py_sleep function
240- let example = py . import (" example" )? ;
240+ let example = py . import_bound (" example" )? ;
241241
242242 // calling the py_sleep method like a normal function
243243 // returns a coroutine
@@ -289,7 +289,7 @@ async fn rust_sleep() {
289289}
290290
291291#[pyfunction]
292- fn call_rust_sleep (py : Python ) -> PyResult <& PyAny > {
292+ fn call_rust_sleep (py : Python ) -> PyResult <Bound < PyAny > > {
293293 pyo3_asyncio :: async_std :: future_into_py (py , async move {
294294 rust_sleep (). await ;
295295 Ok (())
@@ -356,7 +356,7 @@ async fn main() -> PyResult<()> {
356356 // PyO3 is initialized - Ready to go
357357
358358 let fut = Python :: with_gil (| py | -> PyResult <_ > {
359- let asyncio = py . import (" asyncio" )? ;
359+ let asyncio = py . import_bound (" asyncio" )? ;
360360
361361 // convert asyncio.sleep into a Rust Future
362362 pyo3_asyncio :: async_std :: into_future (
@@ -443,7 +443,7 @@ tokio = "1.9"
443443use pyo3 :: {prelude :: * , wrap_pyfunction};
444444
445445#[pyfunction]
446- fn rust_sleep (py : Python ) -> PyResult <& PyAny > {
446+ fn rust_sleep (py : Python ) -> PyResult <Bound < PyAny > > {
447447 pyo3_asyncio :: tokio :: future_into_py (py , async {
448448 tokio :: time :: sleep (std :: time :: Duration :: from_secs (1 )). await ;
449449 Ok (())
@@ -504,7 +504,7 @@ fn main() -> PyResult<()> {
504504 pyo3 :: prepare_freethreaded_python ();
505505
506506 Python :: with_gil (| py | {
507- let uvloop = py . import (" uvloop" )? ;
507+ let uvloop = py . import_bound (" uvloop" )? ;
508508 uvloop . call_method0 (" install" )? ;
509509
510510 // store a reference for the assertion
@@ -514,11 +514,11 @@ fn main() -> PyResult<()> {
514514 // verify that we are on a uvloop.Loop
515515 Python :: with_gil (| py | -> PyResult <()> {
516516 assert! (uvloop
517- . as_ref (py )
517+ . bind (py )
518518 . getattr (" Loop" )?
519519 . downcast :: <PyType >()
520520 . unwrap ()
521- . is_instance (pyo3_asyncio :: async_std :: get_current_loop (py )? )? );
521+ . is_instance (& pyo3_asyncio :: async_std :: get_current_loop (py )? )? );
522522 Ok (())
523523 })? ;
524524
@@ -601,24 +601,24 @@ To make things a bit easier, I decided to keep most of the old API alongside the
601601 pyo3 :: prepare_freethreaded_python ();
602602
603603 Python :: with_gil (| py | {
604- let asyncio = py . import (" asyncio" )? ;
604+ let asyncio = py . import_bound (" asyncio" )? ;
605605
606606 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 ,))? ;
608608
609- let event_loop_hdl = PyObject :: from (event_loop );
609+ let event_loop_hdl = PyObject :: from (event_loop . clone () );
610610
611611 pyo3_asyncio :: tokio :: get_runtime (). spawn (async move {
612612 tokio :: time :: sleep (std :: time :: Duration :: from_secs (1 )). await ;
613613
614614 // Stop the event loop manually
615615 Python :: with_gil (| py | {
616616 event_loop_hdl
617- . as_ref (py )
617+ . bind (py )
618618 . call_method1 (
619619 " call_soon_threadsafe" ,
620620 (event_loop_hdl
621- . as_ref (py )
621+ . bind (py )
622622 . getattr (" stop" )
623623 . unwrap (),),
624624 )
0 commit comments