Force server methods to be async and client calls to use await

This commit is contained in:
Lasse Blaauwbroek
2023-06-08 03:56:57 +02:00
parent a69bc72a0b
commit 4b5c4211f1
11 changed files with 193 additions and 310 deletions

View File

@@ -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)