mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-124594: Create and reuse the same context for the entire asyncio REPL session (#124595)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
This commit is contained in:
parent
3e3a4d2315
commit
67e01a430f
3 changed files with 42 additions and 2 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue