22
33import asyncio
44import contextlib
5- import contextvars
65import queue
76import signal
87import socket
@@ -439,12 +438,11 @@ def aiotrio_run(
439438 pass_not_threadsafe : bool = True ,
440439 ** start_guest_run_kwargs : Any ,
441440) -> T :
442- loop = asyncio .new_event_loop ()
443-
444441 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 ()
446444
447- def trio_done_callback (main_outcome : Outcome [object ]) -> None :
445+ def trio_done_callback (main_outcome : Outcome [T ]) -> None :
448446 print (f"trio_fn finished: { main_outcome !r} " )
449447 trio_done_fut .set_result (main_outcome )
450448
@@ -458,12 +456,9 @@ def trio_done_callback(main_outcome: Outcome[object]) -> None:
458456 ** start_guest_run_kwargs ,
459457 )
460458
461- return (await trio_done_fut ).unwrap () # type: ignore[no-any-return]
459+ return (await trio_done_fut ).unwrap ()
462460
463- try :
464- return loop .run_until_complete (aio_main ())
465- finally :
466- loop .close ()
461+ return asyncio .run (aio_main ())
467462
468463
469464def test_guest_mode_on_asyncio () -> None :
@@ -659,10 +654,7 @@ async def trio_main() -> None:
659654
660655 gc_collect_harder ()
661656
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 )
666658
667659 assert record == {("asyncio" , "asyncio" ), ("trio" , "trio" )}
668660
@@ -707,9 +699,6 @@ async def trio_main() -> None:
707699
708700 gc_collect_harder ()
709701
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 )
714703
715704 assert record == {("asyncio" , "asyncio" , True ), ("trio" , "trio" , True )}
0 commit comments