Merge pull request #7538 from thunderboltsid/pypy-experiments

Change Python one-time grpc_init/grpc_shutdown to per-object-lifetime grpc_init/grpc_shutdown
pull/7741/head
kpayson64 9 years ago committed by GitHub
commit b473c79df3
  1. 2
      src/python/grpcio/grpc/_cython/_cygrpc/call.pyx.pxi
  2. 2
      src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi
  3. 2
      src/python/grpcio/grpc/_cython/_cygrpc/completion_queue.pyx.pxi
  4. 14
      src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi
  5. 12
      src/python/grpcio/grpc/_cython/_cygrpc/records.pyx.pxi
  6. 2
      src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
  7. 4
      src/python/grpcio/grpc/_cython/cygrpc.pyx

@ -34,6 +34,7 @@ cdef class Call:
def __cinit__(self): def __cinit__(self):
# Create an *empty* call # Create an *empty* call
grpc_init()
self.c_call = NULL self.c_call = NULL
self.references = [] self.references = []
@ -106,6 +107,7 @@ cdef class Call:
def __dealloc__(self): def __dealloc__(self):
if self.c_call != NULL: if self.c_call != NULL:
grpc_call_destroy(self.c_call) grpc_call_destroy(self.c_call)
grpc_shutdown()
# The object *should* always be valid from Python. Used for debugging. # The object *should* always be valid from Python. Used for debugging.
@property @property

@ -34,6 +34,7 @@ cdef class Channel:
def __cinit__(self, bytes target, ChannelArgs arguments=None, def __cinit__(self, bytes target, ChannelArgs arguments=None,
ChannelCredentials channel_credentials=None): ChannelCredentials channel_credentials=None):
grpc_init()
cdef grpc_channel_args *c_arguments = NULL cdef grpc_channel_args *c_arguments = NULL
cdef char *c_target = NULL cdef char *c_target = NULL
self.c_channel = NULL self.c_channel = NULL
@ -103,3 +104,4 @@ cdef class Channel:
def __dealloc__(self): def __dealloc__(self):
if self.c_channel != NULL: if self.c_channel != NULL:
grpc_channel_destroy(self.c_channel) grpc_channel_destroy(self.c_channel)
grpc_shutdown()

@ -38,6 +38,7 @@ cdef int _INTERRUPT_CHECK_PERIOD_MS = 200
cdef class CompletionQueue: cdef class CompletionQueue:
def __cinit__(self): def __cinit__(self):
grpc_init()
with nogil: with nogil:
self.c_completion_queue = grpc_completion_queue_create(NULL) self.c_completion_queue = grpc_completion_queue_create(NULL)
self.is_shutting_down = False self.is_shutting_down = False
@ -129,3 +130,4 @@ cdef class CompletionQueue:
self.c_completion_queue, c_deadline, NULL) self.c_completion_queue, c_deadline, NULL)
self._interpret_event(event) self._interpret_event(event)
grpc_completion_queue_destroy(self.c_completion_queue) grpc_completion_queue_destroy(self.c_completion_queue)
grpc_shutdown()

@ -33,6 +33,7 @@ cimport cpython
cdef class ChannelCredentials: cdef class ChannelCredentials:
def __cinit__(self): def __cinit__(self):
grpc_init()
self.c_credentials = NULL self.c_credentials = NULL
self.c_ssl_pem_key_cert_pair.private_key = NULL self.c_ssl_pem_key_cert_pair.private_key = NULL
self.c_ssl_pem_key_cert_pair.certificate_chain = NULL self.c_ssl_pem_key_cert_pair.certificate_chain = NULL
@ -47,11 +48,13 @@ cdef class ChannelCredentials:
def __dealloc__(self): def __dealloc__(self):
if self.c_credentials != NULL: if self.c_credentials != NULL:
grpc_channel_credentials_release(self.c_credentials) grpc_channel_credentials_release(self.c_credentials)
grpc_shutdown()
cdef class CallCredentials: cdef class CallCredentials:
def __cinit__(self): def __cinit__(self):
grpc_init()
self.c_credentials = NULL self.c_credentials = NULL
self.references = [] self.references = []
@ -64,17 +67,20 @@ cdef class CallCredentials:
def __dealloc__(self): def __dealloc__(self):
if self.c_credentials != NULL: if self.c_credentials != NULL:
grpc_call_credentials_release(self.c_credentials) grpc_call_credentials_release(self.c_credentials)
grpc_shutdown()
cdef class ServerCredentials: cdef class ServerCredentials:
def __cinit__(self): def __cinit__(self):
grpc_init()
self.c_credentials = NULL self.c_credentials = NULL
self.references = [] self.references = []
def __dealloc__(self): def __dealloc__(self):
if self.c_credentials != NULL: if self.c_credentials != NULL:
grpc_server_credentials_release(self.c_credentials) grpc_server_credentials_release(self.c_credentials)
grpc_shutdown()
cdef class CredentialsMetadataPlugin: cdef class CredentialsMetadataPlugin:
@ -90,6 +96,7 @@ cdef class CredentialsMetadataPlugin:
successful). successful).
name (bytes): Plugin name. name (bytes): Plugin name.
""" """
grpc_init()
if not callable(plugin_callback): if not callable(plugin_callback):
raise ValueError('expected callable plugin_callback') raise ValueError('expected callable plugin_callback')
self.plugin_callback = plugin_callback self.plugin_callback = plugin_callback
@ -105,10 +112,14 @@ cdef class CredentialsMetadataPlugin:
cpython.Py_INCREF(self) cpython.Py_INCREF(self)
return result return result
def __dealloc__(self):
grpc_shutdown()
cdef class AuthMetadataContext: cdef class AuthMetadataContext:
def __cinit__(self): def __cinit__(self):
grpc_init()
self.context.service_url = NULL self.context.service_url = NULL
self.context.method_name = NULL self.context.method_name = NULL
@ -120,6 +131,9 @@ cdef class AuthMetadataContext:
def method_name(self): def method_name(self):
return self.context.method_name return self.context.method_name
def __dealloc__(self):
grpc_shutdown()
cdef void plugin_get_metadata( cdef void plugin_get_metadata(
void *state, grpc_auth_metadata_context context, void *state, grpc_auth_metadata_context context,

@ -176,12 +176,14 @@ cdef class Timespec:
cdef class CallDetails: cdef class CallDetails:
def __cinit__(self): def __cinit__(self):
grpc_init()
with nogil: with nogil:
grpc_call_details_init(&self.c_details) grpc_call_details_init(&self.c_details)
def __dealloc__(self): def __dealloc__(self):
with nogil: with nogil:
grpc_call_details_destroy(&self.c_details) grpc_call_details_destroy(&self.c_details)
grpc_shutdown()
@property @property
def method(self): def method(self):
@ -232,6 +234,7 @@ cdef class Event:
cdef class ByteBuffer: cdef class ByteBuffer:
def __cinit__(self, bytes data): def __cinit__(self, bytes data):
grpc_init()
if data is None: if data is None:
self.c_byte_buffer = NULL self.c_byte_buffer = NULL
return return
@ -288,6 +291,7 @@ cdef class ByteBuffer:
def __dealloc__(self): def __dealloc__(self):
if self.c_byte_buffer != NULL: if self.c_byte_buffer != NULL:
grpc_byte_buffer_destroy(self.c_byte_buffer) grpc_byte_buffer_destroy(self.c_byte_buffer)
grpc_shutdown()
cdef class SslPemKeyCertPair: cdef class SslPemKeyCertPair:
@ -319,6 +323,7 @@ cdef class ChannelArg:
cdef class ChannelArgs: cdef class ChannelArgs:
def __cinit__(self, args): def __cinit__(self, args):
grpc_init()
self.args = list(args) self.args = list(args)
for arg in self.args: for arg in self.args:
if not isinstance(arg, ChannelArg): if not isinstance(arg, ChannelArg):
@ -333,6 +338,7 @@ cdef class ChannelArgs:
def __dealloc__(self): def __dealloc__(self):
with nogil: with nogil:
gpr_free(self.c_args.arguments) gpr_free(self.c_args.arguments)
grpc_shutdown()
def __len__(self): def __len__(self):
# self.args is never stale; it's only updated from this file # self.args is never stale; it's only updated from this file
@ -399,6 +405,7 @@ cdef class _MetadataIterator:
cdef class Metadata: cdef class Metadata:
def __cinit__(self, metadata): def __cinit__(self, metadata):
grpc_init()
self.metadata = list(metadata) self.metadata = list(metadata)
for metadatum in metadata: for metadatum in metadata:
if not isinstance(metadatum, Metadatum): if not isinstance(metadatum, Metadatum):
@ -420,6 +427,7 @@ cdef class Metadata:
# it'd be nice if that were documented somewhere...) # it'd be nice if that were documented somewhere...)
# TODO(atash): document this in the C core # TODO(atash): document this in the C core
grpc_metadata_array_destroy(&self.c_metadata_array) grpc_metadata_array_destroy(&self.c_metadata_array)
grpc_shutdown()
def __len__(self): def __len__(self):
return self.c_metadata_array.count return self.c_metadata_array.count
@ -437,6 +445,7 @@ cdef class Metadata:
cdef class Operation: cdef class Operation:
def __cinit__(self): def __cinit__(self):
grpc_init()
self.references = [] self.references = []
self._received_status_details = NULL self._received_status_details = NULL
self._received_status_details_capacity = 0 self._received_status_details_capacity = 0
@ -529,6 +538,7 @@ cdef class Operation:
# This means that we need to clean up after receive_status_on_client. # This means that we need to clean up after receive_status_on_client.
if self.c_op.type == GRPC_OP_RECV_STATUS_ON_CLIENT: if self.c_op.type == GRPC_OP_RECV_STATUS_ON_CLIENT:
gpr_free(self._received_status_details) gpr_free(self._received_status_details)
grpc_shutdown()
def operation_send_initial_metadata(Metadata metadata, int flags): def operation_send_initial_metadata(Metadata metadata, int flags):
cdef Operation op = Operation() cdef Operation op = Operation()
@ -645,6 +655,7 @@ cdef class _OperationsIterator:
cdef class Operations: cdef class Operations:
def __cinit__(self, operations): def __cinit__(self, operations):
grpc_init()
self.operations = list(operations) # normalize iterable self.operations = list(operations) # normalize iterable
self.c_ops = NULL self.c_ops = NULL
self.c_nops = 0 self.c_nops = 0
@ -667,6 +678,7 @@ cdef class Operations:
def __dealloc__(self): def __dealloc__(self):
with nogil: with nogil:
gpr_free(self.c_ops) gpr_free(self.c_ops)
grpc_shutdown()
def __iter__(self): def __iter__(self):
return _OperationsIterator(self) return _OperationsIterator(self)

@ -35,6 +35,7 @@ import time
cdef class Server: cdef class Server:
def __cinit__(self, ChannelArgs arguments=None): def __cinit__(self, ChannelArgs arguments=None):
grpc_init()
cdef grpc_channel_args *c_arguments = NULL cdef grpc_channel_args *c_arguments = NULL
self.references = [] self.references = []
self.registered_completion_queues = [] self.registered_completion_queues = []
@ -172,3 +173,4 @@ cdef class Server:
while not self.is_shutdown: while not self.is_shutdown:
time.sleep(0) time.sleep(0)
grpc_server_destroy(self.c_server) grpc_server_destroy(self.c_server)
grpc_shutdown()

@ -55,12 +55,8 @@ cdef extern from "Python.h":
def _initialize(): def _initialize():
grpc_init()
grpc_set_ssl_roots_override_callback( grpc_set_ssl_roots_override_callback(
<grpc_ssl_roots_override_callback>ssl_roots_override_callback) <grpc_ssl_roots_override_callback>ssl_roots_override_callback)
if Py_AtExit(grpc_shutdown) != 0:
raise ImportError('failed to register gRPC library shutdown callbacks')
_initialize() _initialize()

Loading…
Cancel
Save