1010 Future ,
1111 Task ,
1212 ensure_future ,
13- new_event_loop ,
14- set_event_loop ,
13+ get_running_loop ,
1514 sleep ,
1615)
1716from contextlib import ExitStack , contextmanager
4948 get_traceback_from_context ,
5049 run_in_executor_with_context ,
5150)
52- from prompt_toolkit .eventloop .utils import call_soon_threadsafe , get_event_loop
51+ from prompt_toolkit .eventloop .utils import call_soon_threadsafe
5352from prompt_toolkit .filters import Condition , Filter , FilterOrBool , to_filter
5453from prompt_toolkit .formatted_text import AnyFormattedText
5554from prompt_toolkit .input .base import Input
@@ -458,7 +457,7 @@ def invalidate(self) -> None:
458457 """
459458 if not self ._is_running :
460459 # Don't schedule a redraw if we're not running.
461- # Otherwise, `get_event_loop ()` in `call_soon_threadsafe` can fail.
460+ # Otherwise, `get_running_loop ()` in `call_soon_threadsafe` can fail.
462461 # See: https://github.com/dbcli/mycli/issues/797
463462 return
464463
@@ -787,7 +786,7 @@ def flush_input() -> None:
787786
788787 @contextmanager
789788 def get_loop () -> Iterator [AbstractEventLoop ]:
790- loop = get_event_loop ()
789+ loop = get_running_loop ()
791790 self .loop = loop
792791
793792 try :
@@ -941,14 +940,6 @@ def run_in_thread() -> None:
941940 )
942941 except BaseException as e :
943942 exception = e
944- finally :
945- # Make sure to close the event loop in this thread. Running
946- # the application creates a new loop (because we're in
947- # another thread), but it doesn't get closed automatically
948- # (also not by the garbage collector).
949- loop = get_event_loop ()
950- loop .run_until_complete (loop .shutdown_asyncgens ())
951- loop .close ()
952943
953944 thread = threading .Thread (target = run_in_thread )
954945 thread .start ()
@@ -958,21 +949,7 @@ def run_in_thread() -> None:
958949 raise exception
959950 return result
960951
961- # We don't create a new event loop by default, because we want to be
962- # sure that when this is called multiple times, each call of `run()`
963- # goes through the same event loop. This way, users can schedule
964- # background-tasks that keep running across multiple prompts.
965- try :
966- loop = get_event_loop ()
967- except RuntimeError :
968- # Possibly we are not running in the main thread, where no event
969- # loop is set by default. Or somebody called `asyncio.run()`
970- # before, which closes the existing event loop. We can create a new
971- # loop.
972- loop = new_event_loop ()
973- set_event_loop (loop )
974-
975- return loop .run_until_complete (
952+ return asyncio .run (
976953 self .run_async (
977954 pre_run = pre_run ,
978955 set_exception_handler = set_exception_handler ,
@@ -1074,7 +1051,7 @@ def create_background_task(
10741051
10751052 This is not threadsafe.
10761053 """
1077- loop = self .loop or get_event_loop ()
1054+ loop = self .loop or get_running_loop ()
10781055 task : asyncio .Task [None ] = loop .create_task (coroutine )
10791056 self ._background_tasks .add (task )
10801057
@@ -1093,7 +1070,7 @@ def _on_background_task_done(self, task: "asyncio.Task[None]") -> None:
10931070
10941071 exc = task .exception ()
10951072 if exc is not None :
1096- get_event_loop ().call_exception_handler (
1073+ get_running_loop ().call_exception_handler (
10971074 {
10981075 "message" : f"prompt_toolkit.Application background task { task !r} "
10991076 "raised an unexpected exception." ,
@@ -1501,7 +1478,7 @@ def attach_winch_signal_handler(
15011478
15021479 # Keep track of the previous handler.
15031480 # (Only UnixSelectorEventloop has `_signal_handlers`.)
1504- loop = get_event_loop ()
1481+ loop = get_running_loop ()
15051482 previous_winch_handler = getattr (loop , "_signal_handlers" , {}).get (sigwinch )
15061483
15071484 try :
0 commit comments