Hi The Cheaterman,
The trick here is that your RPC methods can return a promise rather than a
result. The method call is not considered "done" until the returned promise
completes. If you don't do this, then the method is considered to be done
immediately, and all the promises you created but didn't use are discarded
/ canceled.
In your case you are constructing several promises, one for each client.
So, you also need to join them.
def send(self, message, _context, **kwargs):
message = message.as_builder()
self.chatroom.messages.append(message)
promises = []
for client in self.chatroom.users:
promises.append(client.send(message))
return capnp.join_promises(promises)
Note that if you wanted to do something with the result of the
client.send() calls, you would use .then() to register a callback. You can
see some examples in the calculator server sample here:
https://github.com/capnproto/pycapnp/blob/develop/examples/calculator_server.py
https://github.com/capnproto/pycapnp/blob/develop/examples/calculator.capnp
-Kenton
On Fri, Mar 16, 2018 at 4:37 AM, The Cheaterman <[email protected]>
wrote:
> Hi everyone, I hope you're doing goodie!
>
> First, thanks a lot Kenton for all the magic! I want to use it! However,
> I'd like to use it from Python if possible hehe, but as you may know
> pycapnp is somewhat unmaintained (one thing I'd like to see it have is
> support for more than one client when wrapping a socket FD, or support for
> UDP connection strings somehow, maybe with udp: or something), and that's
> why I'm here instead - I'm probably misunderstanding how to use Capnp RPC
> more than anything else.
>
> So, let's get to the point: I'm trying to learn how to use Capnp RPC in
> Python, so I made what seemed to me like one of the simplest things to do -
> a chat program. I managed to make it work with extremely poor network
> architecture where the client polls the server (several times per second)
> for new messages to be received. The code is here:
> https://github.com/Cheaterman/capnchat/ - the master branch still uses
> the "polling" protocol, while the "push_messages" branch attempts to push
> the messages directly to the client.
>
> The issue happens here: https://github.com/Cheaterman/
> capnchat/blob/push_messages/server.py#L69 (you probably already guessed I
> was trying something like that from the title), which attempts to wait on this
> promise
> <https://github.com/Cheaterman/capnchat/blob/push_messages/server.py#L119>,
> which corresponds to this part
> <https://github.com/Cheaterman/capnchat/blob/push_messages/chatroom.capnp#L23>
> of the proto (implemented here in the client
> <https://github.com/Cheaterman/capnchat/blob/push_messages/client.py#L70>
> - BTW I have no idea if having this ".Server" class on the client is a good
> idea).
>
> As you already mentioned in other threads, the reason this doesn't work is
> because I can't make the main event loop wait for things (AIUI) because
> nothing would be processed in the meantime ; and callbacks are executed in
> the event loop. The problem is, if I don't wait() on that promise, it's
> never actually executed, so I don't quite know what I should be doing
> here... You also mentioned in other threads that I might want to add my
> promise to the TaskSet or something? I don't think this is exposed in
> Python, but I imagine wrapping it wouldn't be an issue (even with my
> limited knowledge of modern C++ and how to use it from Cython).
>
> In any case, thanks in advance for clearing out any misunderstanding I may
> have as to how I am supposed to implement that kind of things. If things go
> well, I'd like to use Capnp RPC to implement a multiplayer games protocol,
> please tell me if that makes sense at all or if I should stop right there
> :-P
>
> Thanks for reading!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> Visit this group at https://groups.google.com/group/capnproto.
>
--
You received this message because you are subscribed to the Google Groups
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/capnproto.