[Bro-Dev] Asynchronous function calls

Robin Sommer robin at icir.org
Mon Sep 21 08:26:45 PDT 2015


With Broker, we have the problem that a number of function calls
operate asynchronously so that one needs to wrap them into "when"
statements, which gets cumbersome quickly. One idea we had discussed
in the past is adding synchronous versions with slightly different
semantics (see [1] for a summary). However, the other day I came
across a new feature in Python 3.5, and I'm now wondering if that
could be a better route for Bro, too.

In a nutshell, Python has added language support for co-routines. From
[2]: "Inside of a coroutine body, the 'await' keyword can be used to
make an expression suspend the execution of the coroutine and wait for
some processing to complete."

Example:

    data = await db.fetch('SELECT ...')

See [2] for more.

I think in Bro we could implement somehting like this through the same
infrastructure that "when" uses (with some tweaks, maybe); but for
user code it would be much nicer. Indeed, existing async BiFs, like
for DNS lookups, could then use this as well.

One challenge here is error handling: what to do if the function
doesn't return / fails / times out? Another question would be
performance: what if we have 1000s of these pending? (I don't think we
know the answer for "when"-stmts either though ...)

Robin

[1] https://www.bro.org/development/projects/broker-extensions.html
[2] http://www.infoq.com/news/2015/05/python-async-await


-- 
Robin Sommer * ICSI/LBNL * robin at icir.org * www.icir.org/robin


More information about the bro-dev mailing list