Hi Ryan, Cap'n Proto uses a single-threaded event loop model. In order for it to be able to perform network I/O, it's necessary that the program returns to the event loop periodically.
If you need to run a function that continuously executes for a long time, then you will need to arrange for that function to execute in a different thread. Unfortunately, it will not be possible to call into Cap'n Proto directly, because Cap'n Proto is not designed to be multi-threaded. So, you will have to arrange some other mechanism by which you communicate events back to the Cap'n Proto thread. Unfortunately, that's going to be somewhat difficult, since you need some way to wake up the Cap'n Proto thread from its event loop on-demand. In C++, I would create a pipe (or on Linux, an eventfd), have the capnp thread listen for events on one end of the pipe, and have the other thread write to the other end to generate an event. But, I'm not sure if pycapnp exposes the low-level KJ event loop API needed to listen on such a pipe. :/ Another possibility might be to have the side thread actually act as a Cap'n Proto client itself, which connects back to the main thread. You could then make RPC calls and use .wait() to give Cap'n Proto a chance to do the (thread-to-thread) network I/O. It's ugly but it might work. Sorry that this use case isn't well-supported right now in Python. Unfortunately the pycapnp implementation hasn't had an active maintainer for some time... -Kenton On Mon, Nov 19, 2018 at 10:20 AM Ryan Taus <[email protected]> wrote: > I am trying to use pycapnp to allow a client to ask a server to do a long > running function. During this function, I want the server to send the > client some feedback messages. My capnp file looks something like this: > > interface Performer { > > > performFunction @0 (); > > > bind @1 (watcher: Watcher); > > > } > > > interface Watcher { > > > update @0 (feedback: Text); > > > } > > > > I can bind my watchers to the performers successfully using TwoParty > Server/Client. However, when I call performFunction I do not get any update > messages back until the performFunction exits, and all the updates are > dumped out at once. My desired outcome is for the watcher to be notified of > the updates each iteration (each time the Performer calls the Watcher's > update function). > > I assume this is because the performfunction is blocking the capnp thread. > > If I run the update in a different thread I get an error: > kj/async.c++:227: failed: expected head == nullptr; EventLoop destroyed > with events still in the queue. Memory leak?;. > If I run the update in a different thread that I call > `capnp.create_event_loop(threaded=True)` in The funciton is performed, but > the Watcher never receives updates. > > Any ideas on how I can get this type of feedback to work with pycapnp? > > -- > 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.
