Mixing event loops

20 views
Skip to first unread message

Owen Healy

unread,
Feb 11, 2021, 2:29:10 AMFeb 11
to Cap'n Proto
So, this started out as a practical problem, but now it's more curiosity.

Cap'n Proto uses an event loop for concurrency. I'm using another library that has its own event loop for concurrency. They refuse to use each other's event loops. Although the basic logic is the same between the two, the APIs are incompatible.

So, how do I mix two event loops?

My thought was, that if one of the event loops supports a callback when an event is queued I can essentially use one event loop to drive the other.

The basic logic would be something like the following (Python syntax, but you get the idea):

loop1 = cnp_event_loop()
loop2 = some_other_lib_event_loop()

@loop1.on_event_added
def _():
    @loop2.call_soon
    def _():
        loop1.poll()

def adapt_future(f):
    f2 = loop2.create_future()
    if f.done():
        f2.set_result(f.result())
    else:
        @f.add_done_callback
        def _():
            f2.set_result(f.result())
    return f2

async def main():
    res = await adapt_future(func_that_returns_a_cnp_promise())
    print(res)

loop2.run_until_complete(main2())

(The function Cap'n Proto doesn't support is on_event_added() on an EventLoop.)

Just curious if anyone has any thoughts on this.

Kenton Varda

unread,
Feb 11, 2021, 11:25:48 AMFeb 11
to Owen Healy, Cap'n Proto
Hi Owen,

KJ is actually designed to allow its event loop to sit on top of some other event loop, for this very reason. But, it's not easy. You have to implement a custom subclass of `kj::EventPort` and probably all of the interfaces (that you care about) in `kj/async-io.h`.

The node-capnp implementation contains an example of this, placing the KJ event loop on top of libuv. See the first part of this file:


I've often thought it would be cool if the KJ codebase itself contained adaptors for various popular async frameworks, but never had time to work on that...

-Kenton

--
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 capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/7b46e93a-a42e-4f69-b5f5-f924a3b81550n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages