@@ -163,8 +163,9 @@ impl Website {
163
163
let mut rx2 = website
164
164
. subscribe ( * BUFFER / 2 )
165
165
. expect ( "sync feature should be enabled" ) ;
166
+ let rt = pyo3_async_runtimes:: tokio:: get_runtime ( ) ;
166
167
167
- let handle = spider :: tokio :: spawn ( async move {
168
+ let handle = rt . spawn ( async move {
168
169
while let Ok ( res) = rx2. recv ( ) . await {
169
170
let page = new_page ( & res, raw_content) ;
170
171
Python :: with_gil ( |py| {
@@ -178,7 +179,7 @@ impl Website {
178
179
_ => 0 ,
179
180
} ;
180
181
181
- let crawl_handle = spider :: tokio :: spawn ( async move {
182
+ let crawl_handle = rt . spawn ( async move {
182
183
if headless {
183
184
website. crawl ( ) . await ;
184
185
} else {
@@ -237,8 +238,9 @@ impl Website {
237
238
Some ( handle) => handle. 0 + 1 ,
238
239
_ => 0 ,
239
240
} ;
241
+ let rt = pyo3_async_runtimes:: tokio:: get_runtime ( ) ;
240
242
241
- let crawl_handle = spider :: tokio :: spawn ( async move {
243
+ let crawl_handle = rt . spawn ( async move {
242
244
if headless {
243
245
website. crawl ( ) . await ;
244
246
} else {
@@ -283,8 +285,9 @@ impl Website {
283
285
let mut rx2 = website
284
286
. subscribe ( * BUFFER / 2 )
285
287
. expect ( "sync feature should be enabled" ) ;
288
+ let rt = pyo3_async_runtimes:: tokio:: get_runtime ( ) ;
286
289
287
- let handle = spider :: tokio :: spawn ( async move {
290
+ let handle = rt . spawn ( async move {
288
291
while let Ok ( res) = rx2. recv ( ) . await {
289
292
let page = new_page ( & res, raw_content) ;
290
293
Python :: with_gil ( |py| {
@@ -298,7 +301,7 @@ impl Website {
298
301
_ => 0 ,
299
302
} ;
300
303
301
- let crawl_handle = spider :: tokio :: spawn ( async move {
304
+ let crawl_handle = rt . spawn ( async move {
302
305
website. crawl_smart ( ) . await ;
303
306
} ) ;
304
307
@@ -344,13 +347,14 @@ impl Website {
344
347
_ => {
345
348
if background {
346
349
let mut website = slf. inner . clone ( ) ;
350
+ let rt = pyo3_async_runtimes:: tokio:: get_runtime ( ) ;
347
351
348
352
let crawl_id = match slf. crawl_handles . last ( ) {
349
353
Some ( handle) => handle. 0 + 1 ,
350
354
_ => 0 ,
351
355
} ;
352
356
353
- let crawl_handle = spider :: tokio :: spawn ( async move {
357
+ let crawl_handle = rt . spawn ( async move {
354
358
website. crawl_smart ( ) . await ;
355
359
} ) ;
356
360
@@ -388,8 +392,9 @@ impl Website {
388
392
let mut rx2 = website
389
393
. subscribe ( * BUFFER / 2 )
390
394
. expect ( "sync feature should be enabled" ) ;
395
+ let rt = pyo3_async_runtimes:: tokio:: get_runtime ( ) ;
391
396
392
- let handle = spider :: tokio :: spawn ( async move {
397
+ let handle = rt . spawn ( async move {
393
398
while let Ok ( res) = rx2. recv ( ) . await {
394
399
let page = new_page ( & res, raw_content) ;
395
400
@@ -405,7 +410,7 @@ impl Website {
405
410
_ => 0 ,
406
411
} ;
407
412
408
- let crawl_handle = spider :: tokio :: spawn ( async move {
413
+ let crawl_handle = rt . spawn ( async move {
409
414
if headless {
410
415
website. scrape ( ) . await ;
411
416
} else {
@@ -459,13 +464,14 @@ impl Website {
459
464
_ => {
460
465
if background {
461
466
let mut website = slf. inner . clone ( ) ;
467
+ let rt = pyo3_async_runtimes:: tokio:: get_runtime ( ) ;
462
468
463
469
let crawl_id = match slf. crawl_handles . last ( ) {
464
470
Some ( handle) => handle. 0 + 1 ,
465
471
_ => 0 ,
466
472
} ;
467
473
468
- let crawl_handle = spider :: tokio :: spawn ( async move {
474
+ let crawl_handle = rt . spawn ( async move {
469
475
if headless {
470
476
website. scrape ( ) . await ;
471
477
} else {
@@ -498,8 +504,9 @@ impl Website {
498
504
. subscribe ( * BUFFER / 2 )
499
505
. expect ( "sync feature should be enabled" ) ;
500
506
let raw_content = slf. raw_content ;
507
+ let rt = pyo3_async_runtimes:: tokio:: get_runtime ( ) ;
501
508
502
- let handler = spider :: tokio :: spawn ( async move {
509
+ let handler = rt . spawn ( async move {
503
510
while let Ok ( res) = rx2. recv ( ) . await {
504
511
Python :: with_gil ( |py| {
505
512
let _ = callback. call ( py, ( new_page ( & res, raw_content) , ) , None ) ;
@@ -603,28 +610,19 @@ impl Website {
603
610
let py = slf. py ( ) ;
604
611
let dict = obj. downcast_bound :: < pyo3:: types:: PyDict > ( py) ;
605
612
606
- match dict {
607
- Ok ( keys) => {
608
- for key in keys. into_iter ( ) {
609
- let header_key = spider:: reqwest:: header:: HeaderName :: from_str ( & key. 0 . to_string ( ) ) ;
610
-
611
- match header_key {
612
- Ok ( hn) => {
613
- let header_value = key. 1 . to_string ( ) ;
614
-
615
- match spider:: reqwest:: header:: HeaderValue :: from_str ( & header_value) {
616
- Ok ( hk) => {
617
- h. append ( hn, hk) ;
618
- }
619
- _ => ( ) ,
620
- }
621
- }
622
- _ => ( ) ,
613
+ if let Ok ( keys) = dict {
614
+ for key in keys. into_iter ( ) {
615
+ let header_key = spider:: reqwest:: header:: HeaderName :: from_str ( & key. 0 . to_string ( ) ) ;
616
+
617
+ if let Ok ( hn) = header_key {
618
+ let header_value = key. 1 . to_string ( ) ;
619
+
620
+ if let Ok ( hk) = spider:: reqwest:: header:: HeaderValue :: from_str ( & header_value) {
621
+ h. append ( hn, hk) ;
623
622
}
624
623
}
625
- slf. inner . with_headers ( Some ( h) ) ;
626
624
}
627
- _ => ( ) ,
625
+ slf . inner . with_headers ( Some ( h ) ) ;
628
626
}
629
627
}
630
628
_ => {
0 commit comments