From 2f0f03d1a84b509c8000929293a5354c4956834f Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Wed, 11 Dec 2013 22:25:43 -0800 Subject: [PATCH] Add TCP_NODELAY --- examples/calculator_client.py | 4 ++++ examples/calculator_server.py | 17 +++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/examples/calculator_client.py b/examples/calculator_client.py index 0b7e3ac..bc8adca 100755 --- a/examples/calculator_client.py +++ b/examples/calculator_client.py @@ -285,4 +285,8 @@ if __name__ == '__main__': host, port = parse_args().host.split(':') sock = socket.create_connection((host, port)) + + # Set TCP_NODELAY on socket to disable Nagle's algorithm. This is not + # neccessary, but it speeds things up. + sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) main(sock) diff --git a/examples/calculator_server.py b/examples/calculator_server.py index bcfcdd2..1532d95 100755 --- a/examples/calculator_server.py +++ b/examples/calculator_server.py @@ -9,7 +9,7 @@ import capnp import calculator_capnp -def readValue(value): +def read_value(value): '''Helper function to asynchronously call read() on a Calculator::Value and return a promise for the result. (In the future, the generated code might include something like this automatically.)''' @@ -17,7 +17,7 @@ def readValue(value): return value.read().then(lambda result: result.value) -def evaluateImpl(expression, params=None): +def evaluate_impl(expression, params=None): '''Implementation of CalculatorImpl::evaluate(), also shared by FunctionImpl::call(). In the latter case, `params` are the parameter values passed to the function; in the former case, `params` is just an @@ -28,7 +28,7 @@ def evaluateImpl(expression, params=None): if which == 'literal': return capnp.Promise(expression.literal) elif which == 'previousResult': - return readValue(expression.previousResult) + return read_value(expression.previousResult) elif which == 'parameter': assert expression.parameter < len(params) return capnp.Promise(params[expression.parameter]) @@ -37,7 +37,7 @@ def evaluateImpl(expression, params=None): func = call.function # Evaluate each parameter. - paramPromises = [evaluateImpl(param, params) for param in call.params] + paramPromises = [evaluate_impl(param, params) for param in call.params] joinedParams = capnp.join_promises(paramPromises) # When the parameters are complete, call the function. @@ -78,7 +78,7 @@ class FunctionImpl(calculator_capnp.Calculator.Function.Server): assert len(params) == self.paramCount # using setattr because '=' is not allowed inside of lambdas - return evaluateImpl(self.body, params).then(lambda value: setattr(_context.results, 'value', value)) + return evaluate_impl(self.body, params).then(lambda value: setattr(_context.results, 'value', value)) class OperatorImpl(calculator_capnp.Calculator.Function.Server): @@ -111,7 +111,7 @@ class CalculatorImpl(calculator_capnp.Calculator.Server): "Implementation of the Calculator Cap'n Proto interface." def evaluate(self, expression, _context, **kwargs): - return evaluateImpl(expression).then(lambda value: setattr(_context.results, 'value', ValueImpl(value))) + return evaluate_impl(expression).then(lambda value: setattr(_context.results, 'value', ValueImpl(value))) def defFunction(self, paramCount, body, _context, **kwargs): return FunctionImpl(paramCount, body) @@ -158,6 +158,11 @@ def main(): print("Listening on port: {}".format(port)) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + # Set TCP_NODELAY on socket to disable Nagle's algorithm. This is not + # neccessary, but it speeds things up. + s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) + s.bind((address, port)) s.listen(1) # service only 1 client at a time