aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/grpcio/py3/grpc/_cython/_cygrpc/thread.pyx.pxi
blob: be4cb8b9a8eb1a729ac56b0f9323c9203766ff94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Copyright 2020 The gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def _contextvars_supported():
    """Determines if the contextvars module is supported.

    We use a 'try it and see if it works approach' here rather than predicting
    based on interpreter version in order to support older interpreters that
    may have a backported module based on, e.g. `threading.local`.

    Returns:
      A bool indicating whether `contextvars` are supported in the current
      environment.
    """
    try:
        import contextvars
        return True
    except ImportError:
        return False


def _run_with_context(target):
    """Runs a callable with contextvars propagated.

    If contextvars are supported, the calling thread's context will be copied
    and propagated. If they are not supported, this function is equivalent
    to the identity function.

    Args:
      target: A callable object to wrap.
    Returns:
      A callable object with the same signature as `target` but with
        contextvars propagated.
    """


if _contextvars_supported():
    import contextvars
    def _run_with_context(target):
        ctx = contextvars.copy_context()
        def _run(*args):
            ctx.run(target, *args)
        return _run
else:
    def _run_with_context(target):
        def _run(*args):
            target(*args)
        return _run