Skip to content

Commit 233a818

Browse files
Use existing eventloop in Application.run() if one was installed.
1 parent 5a1484b commit 233a818

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/prompt_toolkit/application/application.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -943,13 +943,22 @@ def run_in_thread() -> None:
943943
raise exception
944944
return result
945945

946-
return asyncio.run(
947-
self.run_async(
948-
pre_run=pre_run,
949-
set_exception_handler=set_exception_handler,
950-
handle_sigint=handle_sigint,
951-
)
946+
coro = self.run_async(
947+
pre_run=pre_run,
948+
set_exception_handler=set_exception_handler,
949+
handle_sigint=handle_sigint,
952950
)
951+
try:
952+
# See whether a loop was installed already. If so, use that. That's
953+
# required for the input hooks to work, they are installed using
954+
# `set_event_loop`.
955+
loop = asyncio.get_event_loop()
956+
except RuntimeError:
957+
# No loop installed. Run like usual.
958+
return asyncio.run(coro)
959+
else:
960+
# Use existing loop.
961+
return loop.run_until_complete(coro)
953962

954963
def _handle_exception(
955964
self, loop: AbstractEventLoop, context: dict[str, Any]

0 commit comments

Comments
 (0)