Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for changing the context of a running task. #2086

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/source/reference-lowlevel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,20 @@ Task API
:func:`wait_task_rescheduled` for details.)


Low-level context tools
=======================

You can change the current :class:`contextvars.Context` a task is running
in with a helper context manager.

.. autofunction:: set_current_context
:async-with:

.. note::

This context manager introduces a scheduling point but not a cancellation
point.

.. _guest-mode:

Using "guest mode" to run Trio on top of other event loops
Expand Down
2 changes: 2 additions & 0 deletions newsfragments/2086.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add the :func:`trio.lowlevel.set_current_context` API to allow changing the
current :class:`contextvars.Context` a task is running in.
2 changes: 2 additions & 0 deletions trio/_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@

from ._mock_clock import MockClock

from ._context import set_current_context

# Windows imports
if sys.platform == "win32":
from ._run import (
Expand Down
21 changes: 21 additions & 0 deletions trio/_core/_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from async_generator import asynccontextmanager

from . import _run


@asynccontextmanager
async def set_current_context(context):
"""Returns an asynchronous context manager to change the
:class:`contextvars.Context` for the current task.

"""
task = _run.current_task()
saved_context = task.context
task.context = context

try:
await _run.cancel_shielded_checkpoint()
yield
finally:
task.context = saved_context
await _run.cancel_shielded_checkpoint()
31 changes: 31 additions & 0 deletions trio/_core/tests/test_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from contextvars import ContextVar, copy_context

from .._run import CancelScope
from .._context import set_current_context

var = ContextVar("var")


async def test_context_change():
var.set("abc")

new_context = copy_context()
async with set_current_context(new_context):
assert var.get() == "abc"
var.set("def")
assert var.get() == "def"

assert var.get() == "abc"


async def test_context_change_with_cancellation():
var.set("qqq")

new_context = copy_context()
with CancelScope() as scope:
async with set_current_context(new_context):
var.set("ghj")
assert var.get() == "ghj"
scope.cancel()

assert var.get() == "qqq"
1 change: 1 addition & 0 deletions trio/lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
notify_closing,
start_thread_soon,
start_guest_run,
set_current_context,
)

from ._subprocess import open_process
Expand Down