Force server methods to be async and client calls to use await
This commit is contained in:
@@ -13,7 +13,7 @@ class PowerFunction(calculator_capnp.Calculator.Function.Server):
|
||||
we're implementing this on the client side and will pass a reference to
|
||||
the server. The server will then be able to make calls back to the client."""
|
||||
|
||||
def call(self, params, **kwargs):
|
||||
async def call(self, params, **kwargs):
|
||||
"""Note the **kwargs. This is very necessary to include, since
|
||||
protocols can add parameters over time. Also, by default, a _context
|
||||
variable is passed to all server methods, but you can also return
|
||||
|
||||
@@ -48,7 +48,7 @@ class ValueImpl(calculator_capnp.Calculator.Value.Server):
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def read(self, **kwargs):
|
||||
async def read(self, **kwargs):
|
||||
return self.value
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ class OperatorImpl(calculator_capnp.Calculator.Function.Server):
|
||||
def __init__(self, op):
|
||||
self.op = op
|
||||
|
||||
def call(self, params, **kwargs):
|
||||
async def call(self, params, **kwargs):
|
||||
assert len(params) == 2
|
||||
|
||||
op = self.op
|
||||
@@ -102,10 +102,10 @@ class CalculatorImpl(calculator_capnp.Calculator.Server):
|
||||
async def evaluate(self, expression, _context, **kwargs):
|
||||
return ValueImpl(await evaluate_impl(expression))
|
||||
|
||||
def defFunction(self, paramCount, body, _context, **kwargs):
|
||||
async def defFunction(self, paramCount, body, _context, **kwargs):
|
||||
return FunctionImpl(paramCount, body)
|
||||
|
||||
def getOperator(self, op, **kwargs):
|
||||
async def getOperator(self, op, **kwargs):
|
||||
return OperatorImpl(op)
|
||||
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class PowerFunction(calculator_capnp.Calculator.Function.Server):
|
||||
we're implementing this on the client side and will pass a reference to
|
||||
the server. The server will then be able to make calls back to the client."""
|
||||
|
||||
def call(self, params, **kwargs):
|
||||
async def call(self, params, **kwargs):
|
||||
"""Note the **kwargs. This is very necessary to include, since
|
||||
protocols can add parameters over time. Also, by default, a _context
|
||||
variable is passed to all server methods, but you can also return
|
||||
|
||||
@@ -17,15 +17,7 @@ logger.setLevel(logging.DEBUG)
|
||||
this_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
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.)"""
|
||||
|
||||
return value.read().then(lambda result: result.value)
|
||||
|
||||
|
||||
def evaluate_impl(expression, params=None):
|
||||
async 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
|
||||
@@ -34,26 +26,23 @@ def evaluate_impl(expression, params=None):
|
||||
which = expression.which()
|
||||
|
||||
if which == "literal":
|
||||
return capnp.Promise(expression.literal)
|
||||
return expression.literal
|
||||
elif which == "previousResult":
|
||||
return read_value(expression.previousResult)
|
||||
return (await expression.previousResult.read()).value
|
||||
elif which == "parameter":
|
||||
assert expression.parameter < len(params)
|
||||
return capnp.Promise(params[expression.parameter])
|
||||
return params[expression.parameter]
|
||||
elif which == "call":
|
||||
call = expression.call
|
||||
func = call.function
|
||||
|
||||
# Evaluate each parameter.
|
||||
paramPromises = [evaluate_impl(param, params) for param in call.params]
|
||||
vals = await asyncio.gather(*paramPromises)
|
||||
|
||||
joinedParams = capnp.join_promises(paramPromises)
|
||||
# When the parameters are complete, call the function.
|
||||
ret = joinedParams.then(lambda vals: func.call(vals)).then(
|
||||
lambda result: result.value
|
||||
)
|
||||
|
||||
return ret
|
||||
result = await func.call(vals)
|
||||
return result.value
|
||||
else:
|
||||
raise ValueError("Unknown expression type: " + which)
|
||||
|
||||
@@ -64,7 +53,7 @@ class ValueImpl(calculator_capnp.Calculator.Value.Server):
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def read(self, **kwargs):
|
||||
async def read(self, **kwargs):
|
||||
return self.value
|
||||
|
||||
|
||||
@@ -77,17 +66,14 @@ class FunctionImpl(calculator_capnp.Calculator.Function.Server):
|
||||
self.paramCount = paramCount
|
||||
self.body = body.as_builder()
|
||||
|
||||
def call(self, params, _context, **kwargs):
|
||||
async def call(self, params, _context, **kwargs):
|
||||
"""Note that we're returning a Promise object here, and bypassing the
|
||||
helper functionality that normally sets the results struct from the
|
||||
returned object. Instead, we set _context.results directly inside of
|
||||
another promise"""
|
||||
|
||||
assert len(params) == self.paramCount
|
||||
# using setattr because '=' is not allowed inside of lambdas
|
||||
return evaluate_impl(self.body, params).then(
|
||||
lambda value: setattr(_context.results, "value", value)
|
||||
)
|
||||
return await evaluate_impl(self.body, params)
|
||||
|
||||
|
||||
class OperatorImpl(calculator_capnp.Calculator.Function.Server):
|
||||
@@ -98,7 +84,7 @@ class OperatorImpl(calculator_capnp.Calculator.Function.Server):
|
||||
def __init__(self, op):
|
||||
self.op = op
|
||||
|
||||
def call(self, params, **kwargs):
|
||||
async def call(self, params, **kwargs):
|
||||
assert len(params) == 2
|
||||
|
||||
op = self.op
|
||||
@@ -118,15 +104,13 @@ class OperatorImpl(calculator_capnp.Calculator.Function.Server):
|
||||
class CalculatorImpl(calculator_capnp.Calculator.Server):
|
||||
"Implementation of the Calculator Cap'n Proto interface."
|
||||
|
||||
def evaluate(self, expression, _context, **kwargs):
|
||||
return evaluate_impl(expression).then(
|
||||
lambda value: setattr(_context.results, "value", ValueImpl(value))
|
||||
)
|
||||
async def evaluate(self, expression, _context, **kwargs):
|
||||
return ValueImpl(await evaluate_impl(expression))
|
||||
|
||||
def defFunction(self, paramCount, body, _context, **kwargs):
|
||||
async def defFunction(self, paramCount, body, _context, **kwargs):
|
||||
return FunctionImpl(paramCount, body)
|
||||
|
||||
def getOperator(self, op, **kwargs):
|
||||
async def getOperator(self, op, **kwargs):
|
||||
return OperatorImpl(op)
|
||||
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ class ExampleImpl(thread_capnp.Example.Server):
|
||||
async def longRunning(self, **kwargs):
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
def alive(self, **kwargs):
|
||||
async def alive(self, **kwargs):
|
||||
return True
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user