Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to return a concrete value and a promise when implementing a server #299

Open
edevil opened this issue Oct 13, 2022 · 0 comments
Open

Comments

@edevil
Copy link
Contributor

edevil commented Oct 13, 2022

I wanted to implement the server side of a capnproto schema that has a method that returns 2 values. The first value is a capability that can be used to send some data, and the second value is a promise of something that depends on this received data.

Is this possible to do with this library?

Example schema:

interface EventDispatcher {
  dispatch @0 (metadata :Metadata) -> (data :ByteStream, response :Response);
}

interface ByteStream {
  write @0 (bytes :Data) -> stream;
  end @1 ();
}

I've tried using a PromiseFulfillerPair but if I return this no progress is made, and the rpc just stalls:

    class ByteStreamImpl(example_capnp.ByteStream.Server):
        def __init__(self, promise):
            super().__init__()
            self.data = b''
            self.promise = promise

        def write(self, bytes, _context, **kwargs):
            self.data += bytes
            return None

        def end(self, _context, **kwargs):
            self.promise.fulfill()

    class DispatcherImpl(example_capnp.EventDispatcher.Server):
        def dispatch(self, metadata, _context, **kwargs):

            p = capnp.PromiseFulfillerPair()
            bstream = ByteStreamImpl(p)

            def set_result(_res):
                response = example_capnp.Response()
                response.outcome = len(bstream.data)
                _context.results.response = response

            _context.results.data = bstream
            return p.promise.then(set_result)

Am I doing something wrong or is this just not possible?
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant