@@ -94,29 +94,27 @@ impl Server {
94
94
let directories = self . directories . clone ( ) ;
95
95
let workers = Arc :: new ( workers) ;
96
96
97
- let asyncio = py. import ( "asyncio" ) . unwrap ( ) ;
97
+ let asyncio = py. import ( "asyncio" ) ?;
98
+ let event_loop = asyncio. call_method0 ( "new_event_loop" ) ?;
99
+ asyncio. call_method1 ( "set_event_loop" , ( event_loop, ) ) ?;
98
100
99
- let event_loop = asyncio. call_method0 ( "new_event_loop" ) . unwrap ( ) ;
100
- asyncio
101
- . call_method1 ( "set_event_loop" , ( event_loop, ) )
102
- . unwrap ( ) ;
103
101
let startup_handler = self . startup_handler . clone ( ) ;
104
102
let shutdown_handler = self . shutdown_handler . clone ( ) ;
105
103
106
- let task_locals = Arc :: new ( pyo3_asyncio:: TaskLocals :: new ( event_loop) . copy_context ( py) ?) ;
107
- let task_locals_cleanup = task_locals. clone ( ) ;
104
+ let task_locals = pyo3_asyncio:: TaskLocals :: new ( event_loop) . copy_context ( py) ?;
105
+ let task_locals_copy = task_locals. clone ( ) ;
108
106
109
107
thread:: spawn ( move || {
110
108
actix_web:: rt:: System :: new ( ) . block_on ( async move {
111
109
debug ! ( "The number of workers are {}" , workers. clone( ) ) ;
112
- execute_event_handler ( startup_handler, & task_locals )
110
+ execute_event_handler ( startup_handler, & task_locals_copy )
113
111
. await
114
112
. unwrap ( ) ;
115
113
116
114
HttpServer :: new ( move || {
117
115
let mut app = App :: new ( ) ;
118
116
119
- let task_locals = task_locals . clone ( ) ;
117
+ let task_locals = task_locals_copy . clone ( ) ;
120
118
let directories = directories. read ( ) . unwrap ( ) ;
121
119
122
120
// this loop matches three types of directory serving
@@ -168,7 +166,7 @@ impl Server {
168
166
global_request_headers,
169
167
body,
170
168
req| {
171
- pyo3_asyncio:: tokio:: scope_local ( ( * task_locals) . clone ( ) , async move {
169
+ pyo3_asyncio:: tokio:: scope_local ( task_locals. clone ( ) , async move {
172
170
index (
173
171
router,
174
172
const_router,
@@ -198,13 +196,12 @@ impl Server {
198
196
debug ! ( "Ctrl c handler" ) ;
199
197
Python :: with_gil ( |py| {
200
198
pyo3_asyncio:: tokio:: run ( py, async move {
201
- execute_event_handler ( shutdown_handler, & task_locals_cleanup )
199
+ execute_event_handler ( shutdown_handler, & task_locals . clone ( ) )
202
200
. await
203
201
. unwrap ( ) ;
204
202
Ok ( ( ) )
205
203
} )
206
- . unwrap ( ) ;
207
- } )
204
+ } ) ?
208
205
}
209
206
Ok ( ( ) )
210
207
}
0 commit comments