@@ -62,20 +62,20 @@ pub use websocket::{
6262
6363/// Handle to a shared Python event loop
6464pub struct EventLoopHandle {
65- event_loop : PyObject ,
65+ event_loop : Py < PyAny > ,
6666}
6767
6868impl EventLoopHandle {
6969 /// Get the Python event loop object
70- pub fn event_loop ( & self ) -> & PyObject {
70+ pub fn event_loop ( & self ) -> & Py < PyAny > {
7171 & self . event_loop
7272 }
7373}
7474
7575impl Drop for EventLoopHandle {
7676 fn drop ( & mut self ) {
7777 // Stop the Python event loop when the last handle is dropped
78- Python :: with_gil ( |py| {
78+ Python :: attach ( |py| {
7979 if let Err ( e) = self . event_loop . bind ( py) . call_method0 ( "stop" ) {
8080 eprintln ! ( "Failed to stop Python event loop: {e}" ) ;
8181 }
@@ -111,10 +111,10 @@ fn create_event_loop_handle() -> Result<EventLoopHandle, HandlerError> {
111111 ensure_python_symbols_global ( ) ;
112112
113113 // Initialize Python if not already initialized
114- pyo3 :: prepare_freethreaded_python ( ) ;
114+ Python :: initialize ( ) ;
115115
116116 // Create event loop
117- let event_loop = Python :: with_gil ( |py| -> Result < PyObject , HandlerError > {
117+ let event_loop = Python :: attach ( |py| -> Result < Py < PyAny > , HandlerError > {
118118 let asyncio = py. import ( "asyncio" ) ?;
119119 let event_loop = asyncio. call_method0 ( "new_event_loop" ) ?;
120120 let event_loop_py = event_loop. unbind ( ) ;
@@ -139,7 +139,7 @@ pub struct Asgi {
139139 // Shared Python event loop handle
140140 event_loop_handle : Arc < EventLoopHandle > ,
141141 // ASGI app function
142- app_function : PyObject ,
142+ app_function : Py < PyAny > ,
143143}
144144
145145unsafe impl Send for Asgi { }
@@ -160,7 +160,7 @@ impl Asgi {
160160 let event_loop_handle = ensure_python_event_loop ( ) ?;
161161
162162 // Load Python app
163- let app_function = Python :: with_gil ( |py| -> Result < PyObject , HandlerError > {
163+ let app_function = Python :: attach ( |py| -> Result < Py < PyAny > , HandlerError > {
164164 // Load and compile Python module
165165 let entrypoint = docroot
166166 . join ( format ! ( "{}.py" , target. file) )
@@ -241,7 +241,7 @@ impl Handler for Asgi {
241241 tokio:: spawn ( collect_response_messages ( tx_receiver, response_tx) ) ;
242242
243243 // Submit the ASGI app call to Python event loop
244- Python :: with_gil ( |py| {
244+ Python :: attach ( |py| {
245245 let scope_py = scope. into_pyobject ( py) ?;
246246 let coro = self
247247 . app_function
@@ -361,8 +361,8 @@ fn setup_python_paths(py: Python, docroot: &Path) -> PyResult<()> {
361361}
362362
363363/// Start a Python thread that runs the event loop forever
364- fn start_python_event_loop_thread ( event_loop : PyObject ) {
365- Python :: with_gil ( |py| {
364+ fn start_python_event_loop_thread ( event_loop : Py < PyAny > ) {
365+ Python :: attach ( |py| {
366366 // Set the event loop for this thread and run it
367367 let asyncio = py. import ( "asyncio" ) ?;
368368 asyncio. call_method1 ( "set_event_loop" , ( event_loop. bind ( py) , ) ) ?;
0 commit comments