Support Windows

pull/23107/head
Richard Belleville 5 years ago
parent 2ed3d6bb5e
commit bc157d5eb5
  1. 22
      src/python/grpcio/grpc/_cython/_cygrpc/fork_posix.pyx.pxi
  2. 3
      src/python/grpcio/grpc/_cython/_cygrpc/fork_windows.pyx.pxi
  3. 60
      src/python/grpcio/grpc/_cython/_cygrpc/thread.pyx.pxi
  4. 2
      src/python/grpcio/grpc/_cython/cygrpc.pyx
  5. 7
      src/python/grpcio_tests/tests/unit/_contextvars_propagation_test.py

@ -94,28 +94,6 @@ def fork_handlers_and_grpc_init():
_fork_state.fork_handler_registered = True
def _contextvars_supported():
try:
import contextvars
return True
except ImportError:
return False
if _contextvars_supported():
import contextvars
def _run_with_context(target):
ctx = contextvars.copy_context()
def _run(*args):
ctx.run(target, *args)
return _run
else:
# NOTE(rbellevi): `contextvars` was not introduced until 3.7. On earlier
# interpreters, we simply do not propagate contextvars between threads.
def _run_with_context(target):
def _run(*args):
target(*args)
return _run
class ForkManagedThread(object):

@ -19,10 +19,9 @@ def fork_handlers_and_grpc_init():
grpc_init()
# TODO: Propagate contextvars.
class ForkManagedThread(object):
def __init__(self, target, args=()):
self._thread = threading.Thread(target=target, args=args)
self._thread = threading.Thread(target=_tun_with_context(target), args=args)
def setDaemon(self, daemonic):
self._thread.daemon = daemonic

@ -0,0 +1,60 @@
# 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.
"""
pass
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

@ -59,6 +59,8 @@ include "_cygrpc/iomgr.pyx.pxi"
include "_cygrpc/grpc_gevent.pyx.pxi"
include "_cygrpc/thread.pyx.pxi"
IF UNAME_SYSNAME == "Windows":
include "_cygrpc/fork_windows.pyx.pxi"
ELSE:

@ -11,7 +11,7 @@
# 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.
"""Test of propagation of contextvars between threads."""
"""Test of propagation of contextvars to AuthMetadataPlugin threads.."""
import contextlib
import logging
@ -30,9 +30,6 @@ def _unary_unary_handler(request, context):
return request
# TODO: Test for <3.7 and 3.7+.
def contextvars_supported():
try:
import contextvars
@ -110,8 +107,6 @@ class ContextVarsPropagationTest(unittest.TestCase):
self.assertEqual(_REQUEST, response)
test_call_credentials.assert_called(self)
# TODO: Test simple unary-unary.
if __name__ == '__main__':
logging.basicConfig()

Loading…
Cancel
Save