2
2
3
3
import asyncio
4
4
import contextlib
5
- import contextvars
6
5
import queue
7
6
import signal
8
7
import socket
@@ -439,12 +438,11 @@ def aiotrio_run(
439
438
pass_not_threadsafe : bool = True ,
440
439
** start_guest_run_kwargs : Any ,
441
440
) -> T :
442
- loop = asyncio .new_event_loop ()
443
-
444
441
async def aio_main () -> T :
445
- trio_done_fut = loop .create_future ()
442
+ loop = asyncio .get_running_loop ()
443
+ trio_done_fut : asyncio .Future [Outcome [T ]] = loop .create_future ()
446
444
447
- def trio_done_callback (main_outcome : Outcome [object ]) -> None :
445
+ def trio_done_callback (main_outcome : Outcome [T ]) -> None :
448
446
print (f"trio_fn finished: { main_outcome !r} " )
449
447
trio_done_fut .set_result (main_outcome )
450
448
@@ -458,12 +456,9 @@ def trio_done_callback(main_outcome: Outcome[object]) -> None:
458
456
** start_guest_run_kwargs ,
459
457
)
460
458
461
- return (await trio_done_fut ).unwrap () # type: ignore[no-any-return]
459
+ return (await trio_done_fut ).unwrap ()
462
460
463
- try :
464
- return loop .run_until_complete (aio_main ())
465
- finally :
466
- loop .close ()
461
+ return asyncio .run (aio_main ())
467
462
468
463
469
464
def test_guest_mode_on_asyncio () -> None :
@@ -659,10 +654,7 @@ async def trio_main() -> None:
659
654
660
655
gc_collect_harder ()
661
656
662
- # Ensure we don't pollute the thread-level context if run under
663
- # an asyncio without contextvars support (3.6)
664
- context = contextvars .copy_context ()
665
- context .run (aiotrio_run , trio_main , host_uses_signal_set_wakeup_fd = True )
657
+ aiotrio_run (trio_main , host_uses_signal_set_wakeup_fd = True )
666
658
667
659
assert record == {("asyncio" , "asyncio" ), ("trio" , "trio" )}
668
660
@@ -707,9 +699,6 @@ async def trio_main() -> None:
707
699
708
700
gc_collect_harder ()
709
701
710
- # Ensure we don't pollute the thread-level context if run under
711
- # an asyncio without contextvars support (3.6)
712
- context = contextvars .copy_context ()
713
- context .run (aiotrio_run , trio_main , host_uses_signal_set_wakeup_fd = True )
702
+ aiotrio_run (trio_main , host_uses_signal_set_wakeup_fd = True )
714
703
715
704
assert record == {("asyncio" , "asyncio" , True ), ("trio" , "trio" , True )}
0 commit comments