diff options
author | Bartosz Sławecki <bartoszpiotrslawecki@gmail.com> | 2024-10-01 16:17:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-01 14:17:22 +0000 |
commit | 67e01a430f4ecfcb540d6a29b347966ff4e53454 (patch) | |
tree | 043e73955ba1aac1a2d1a8ae791be39d796d496f /Lib/asyncio/__main__.py | |
parent | 3e3a4d231518f91ff2f3c5a085b3849e32f1d548 (diff) | |
download | cpython-67e01a430f4ecfcb540d6a29b347966ff4e53454.tar.gz cpython-67e01a430f4ecfcb540d6a29b347966ff4e53454.zip |
gh-124594: Create and reuse the same context for the entire asyncio REPL session (#124595)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Diffstat (limited to 'Lib/asyncio/__main__.py')
-rw-r--r-- | Lib/asyncio/__main__.py | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/Lib/asyncio/__main__.py b/Lib/asyncio/__main__.py index 5120140e061..95c636f9e02 100644 --- a/Lib/asyncio/__main__.py +++ b/Lib/asyncio/__main__.py @@ -1,6 +1,7 @@ import ast import asyncio import concurrent.futures +import contextvars import inspect import os import site @@ -22,6 +23,7 @@ class AsyncIOInteractiveConsole(InteractiveColoredConsole): self.compile.compiler.flags |= ast.PyCF_ALLOW_TOP_LEVEL_AWAIT self.loop = loop + self.context = contextvars.copy_context() def runcode(self, code): global return_code @@ -55,12 +57,12 @@ class AsyncIOInteractiveConsole(InteractiveColoredConsole): return try: - repl_future = self.loop.create_task(coro) + repl_future = self.loop.create_task(coro, context=self.context) futures._chain_future(repl_future, future) except BaseException as exc: future.set_exception(exc) - loop.call_soon_threadsafe(callback) + loop.call_soon_threadsafe(callback, context=self.context) try: return future.result() |