Skip to content

Commit

Permalink
Merge pull request grpc#7538 from thunderboltsid/pypy-experiments
Browse files Browse the repository at this point in the history
Change Python one-time grpc_init/grpc_shutdown to per-object-lifetime grpc_init/grpc_shutdown
  • Loading branch information
kpayson64 authored Aug 15, 2016
2 parents 0c6cce1 + 9eedb4f commit b473c79
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/python/grpcio/grpc/_cython/_cygrpc/call.pyx.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cdef class Call:

def __cinit__(self):
# Create an *empty* call
grpc_init()
self.c_call = NULL
self.references = []

Expand Down Expand Up @@ -106,6 +107,7 @@ cdef class Call:
def __dealloc__(self):
if self.c_call != NULL:
grpc_call_destroy(self.c_call)
grpc_shutdown()

# The object *should* always be valid from Python. Used for debugging.
@property
Expand Down
2 changes: 2 additions & 0 deletions src/python/grpcio/grpc/_cython/_cygrpc/channel.pyx.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cdef class Channel:

def __cinit__(self, bytes target, ChannelArgs arguments=None,
ChannelCredentials channel_credentials=None):
grpc_init()
cdef grpc_channel_args *c_arguments = NULL
cdef char *c_target = NULL
self.c_channel = NULL
Expand Down Expand Up @@ -103,3 +104,4 @@ cdef class Channel:
def __dealloc__(self):
if self.c_channel != NULL:
grpc_channel_destroy(self.c_channel)
grpc_shutdown()
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ cdef int _INTERRUPT_CHECK_PERIOD_MS = 200
cdef class CompletionQueue:

def __cinit__(self):
grpc_init()
with nogil:
self.c_completion_queue = grpc_completion_queue_create(NULL)
self.is_shutting_down = False
Expand Down Expand Up @@ -129,3 +130,4 @@ cdef class CompletionQueue:
self.c_completion_queue, c_deadline, NULL)
self._interpret_event(event)
grpc_completion_queue_destroy(self.c_completion_queue)
grpc_shutdown()
14 changes: 14 additions & 0 deletions src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cimport cpython
cdef class ChannelCredentials:

def __cinit__(self):
grpc_init()
self.c_credentials = NULL
self.c_ssl_pem_key_cert_pair.private_key = NULL
self.c_ssl_pem_key_cert_pair.certificate_chain = NULL
Expand All @@ -47,11 +48,13 @@ cdef class ChannelCredentials:
def __dealloc__(self):
if self.c_credentials != NULL:
grpc_channel_credentials_release(self.c_credentials)
grpc_shutdown()


cdef class CallCredentials:

def __cinit__(self):
grpc_init()
self.c_credentials = NULL
self.references = []

Expand All @@ -64,17 +67,20 @@ cdef class CallCredentials:
def __dealloc__(self):
if self.c_credentials != NULL:
grpc_call_credentials_release(self.c_credentials)
grpc_shutdown()


cdef class ServerCredentials:

def __cinit__(self):
grpc_init()
self.c_credentials = NULL
self.references = []

def __dealloc__(self):
if self.c_credentials != NULL:
grpc_server_credentials_release(self.c_credentials)
grpc_shutdown()


cdef class CredentialsMetadataPlugin:
Expand All @@ -90,6 +96,7 @@ cdef class CredentialsMetadataPlugin:
successful).
name (bytes): Plugin name.
"""
grpc_init()
if not callable(plugin_callback):
raise ValueError('expected callable plugin_callback')
self.plugin_callback = plugin_callback
Expand All @@ -105,10 +112,14 @@ cdef class CredentialsMetadataPlugin:
cpython.Py_INCREF(self)
return result

def __dealloc__(self):
grpc_shutdown()


cdef class AuthMetadataContext:

def __cinit__(self):
grpc_init()
self.context.service_url = NULL
self.context.method_name = NULL

Expand All @@ -120,6 +131,9 @@ cdef class AuthMetadataContext:
def method_name(self):
return self.context.method_name

def __dealloc__(self):
grpc_shutdown()


cdef void plugin_get_metadata(
void *state, grpc_auth_metadata_context context,
Expand Down
12 changes: 12 additions & 0 deletions src/python/grpcio/grpc/_cython/_cygrpc/records.pyx.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,14 @@ cdef class Timespec:
cdef class CallDetails:

def __cinit__(self):
grpc_init()
with nogil:
grpc_call_details_init(&self.c_details)

def __dealloc__(self):
with nogil:
grpc_call_details_destroy(&self.c_details)
grpc_shutdown()

@property
def method(self):
Expand Down Expand Up @@ -232,6 +234,7 @@ cdef class Event:
cdef class ByteBuffer:

def __cinit__(self, bytes data):
grpc_init()
if data is None:
self.c_byte_buffer = NULL
return
Expand Down Expand Up @@ -288,6 +291,7 @@ cdef class ByteBuffer:
def __dealloc__(self):
if self.c_byte_buffer != NULL:
grpc_byte_buffer_destroy(self.c_byte_buffer)
grpc_shutdown()


cdef class SslPemKeyCertPair:
Expand Down Expand Up @@ -319,6 +323,7 @@ cdef class ChannelArg:
cdef class ChannelArgs:

def __cinit__(self, args):
grpc_init()
self.args = list(args)
for arg in self.args:
if not isinstance(arg, ChannelArg):
Expand All @@ -333,6 +338,7 @@ cdef class ChannelArgs:
def __dealloc__(self):
with nogil:
gpr_free(self.c_args.arguments)
grpc_shutdown()

def __len__(self):
# self.args is never stale; it's only updated from this file
Expand Down Expand Up @@ -399,6 +405,7 @@ cdef class _MetadataIterator:
cdef class Metadata:

def __cinit__(self, metadata):
grpc_init()
self.metadata = list(metadata)
for metadatum in metadata:
if not isinstance(metadatum, Metadatum):
Expand All @@ -420,6 +427,7 @@ cdef class Metadata:
# it'd be nice if that were documented somewhere...)
# TODO(atash): document this in the C core
grpc_metadata_array_destroy(&self.c_metadata_array)
grpc_shutdown()

def __len__(self):
return self.c_metadata_array.count
Expand All @@ -437,6 +445,7 @@ cdef class Metadata:
cdef class Operation:

def __cinit__(self):
grpc_init()
self.references = []
self._received_status_details = NULL
self._received_status_details_capacity = 0
Expand Down Expand Up @@ -529,6 +538,7 @@ cdef class Operation:
# This means that we need to clean up after receive_status_on_client.
if self.c_op.type == GRPC_OP_RECV_STATUS_ON_CLIENT:
gpr_free(self._received_status_details)
grpc_shutdown()

def operation_send_initial_metadata(Metadata metadata, int flags):
cdef Operation op = Operation()
Expand Down Expand Up @@ -645,6 +655,7 @@ cdef class _OperationsIterator:
cdef class Operations:

def __cinit__(self, operations):
grpc_init()
self.operations = list(operations) # normalize iterable
self.c_ops = NULL
self.c_nops = 0
Expand All @@ -667,6 +678,7 @@ cdef class Operations:
def __dealloc__(self):
with nogil:
gpr_free(self.c_ops)
grpc_shutdown()

def __iter__(self):
return _OperationsIterator(self)
Expand Down
2 changes: 2 additions & 0 deletions src/python/grpcio/grpc/_cython/_cygrpc/server.pyx.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import time
cdef class Server:

def __cinit__(self, ChannelArgs arguments=None):
grpc_init()
cdef grpc_channel_args *c_arguments = NULL
self.references = []
self.registered_completion_queues = []
Expand Down Expand Up @@ -172,3 +173,4 @@ cdef class Server:
while not self.is_shutdown:
time.sleep(0)
grpc_server_destroy(self.c_server)
grpc_shutdown()
4 changes: 0 additions & 4 deletions src/python/grpcio/grpc/_cython/cygrpc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ cdef extern from "Python.h":


def _initialize():
grpc_init()
grpc_set_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()

0 comments on commit b473c79

Please sign in to comment.