Event Loops

Noah Petherbridge
kirsle
Posted by Noah Petherbridge on Thursday, November 26 2015 @ 04:12:20 PM

This is something I wanted to rant about for a while: event loops in programming.

This post is specifically talking about programming languages that don't have their own built-in async model, and instead left it up to the community to create their own event loop modules instead. And how most of those modules don't get along well with the others.

Story time!

I like to write chatbots. Back when I got started writing bots, I wrote them in Perl and there were modules available for all the most common chat platforms of the time: Net::OSCAR for AOL IM, Net::YMSG for Yahoo! Messenger, Net::IRC and we also had an MSN Messenger module that was simply named MSN.

All of these modules were written to use their respective protocols in a synchronous manner. That is, they didn't use any event loops, but had functions with names like start() and do_one_loop().

For the simplest bots, where you only wanted to sign on one instance of an AIM bot per Perl script, you'd use the start() method which would enter the main loop and block forever. But if you wanted to run multiple bots from the same Perl script (say, sign on three different AIM bots, two MSN bots and one for IRC all at the same time), you could write your own main loop and manually drive the loops of the respective modules.

Since they all somehow agreed on a common API, the code could look like this:

# Where @connections is an array of bot objects, like Net::OSCAR,
# Net::YMSG, Net::IRC, MSN, etc...
while ($running) {
    # main loop
    foreach my $conn (@connections) {
        $conn->do_one_loop();

        # This also frees up the bot to do other background tasks
        # on each loop here.
    }
}

But, more recently I tried writing a new Perl chatbot which I called Aires, and this bot connected to AIM and Yahoo! Messenger (using a newer Net::IM::YMSG module that I and Matt Austin wrote for the new YMSG protocol, since Yahoo shut down their older protocols around the same time Microsoft announced the same plans to shut down old MSNP protocols).

Net::OSCAR and the new Net::IM::YMSG both used the same old synchronous APIs and all was well. But, I wanted Aires to also use XMPP. But the only Perl interface for an XMPP client was AnyEvent::XMPP, and it required the AnyEvent event loop framework.

To make a long story short, if you already have an existing codebase that does a bunch of stuff and doesn't use an event loop framework, and then you want to utilize some module that does use an event loop framework, things don't go very well. It's pretty difficult to mix and match modules when some of them use a certain event framework and the rest of your code doesn't use the same framework.

To support the XMPP module correctly, my entire codebase would have had to be refactored to work in the AnyEvent framework and I'd have to coerce the other two instant messenger modules to work under those conditions.

Event Loops in Procedural Languages

The root problem, I think, is when people try to build event loop architectures on top of otherwise procedural languages, such as Perl and Python. Procedural languages like these aren't event-oriented and just execute their instructions one at a time in the order they were written, and the languages have no built-in "official" way of writing an event-based program.

So, people write their own event loop systems, such as POE or AnyEvent for Perl, or things like twisted and asyncio for Python. Oftentimes the competing event loops are not compatible with each other. Most event loops want to be THE event loop, and don't support some other loop "manually driving" theirs (like calling a do_one_loop() type of function).

A lot of graphical user interface modules (e.g. Tk and GTK+) have their own event loops, which makes sense for them because it makes more sense to write a program that responds to buttons being clicked and scrollbars being scrolled if the entire front-end of your program revolves around a graphical interface. But these have the same problems as any other event loop and it's hard to mix-and-match them, or to use them from an existing codebase that wasn't architected to use a compatible event loop. In the Perl version of Tk there's a way to manually drive Tk's loop (call $mainwindow->update() instead of MainLoop;) but that isn't universally true of all GUI frameworks.

Mixing an arbitrary event loop framework with a GUI event loop isn't always straightforward and is sometimes impossible, unless you want to partition your program into different threads to completely separate two competing event loops from each other.

Another Story!

After trying to bolt on XMPP support via AnyEvent::XMPP in a way that only works some of the time, I abandoned the Perl version of the Aires bot and rewrote it in Python and gave it the same name. I ran into a similar conundrum when trying to mix interfaces that required the Twisted framework (AIM and XMPP) with ones that didn't (local command line interface). I stopped working on this version of Aires as well.

Fast forward quite a bit.

Yesterday I wrote two chatbots from scratch. One was written in Python and I named it Admiral. The goals for this bot, initially, were to connect to both the Slack chat platform and Google Hangouts, using the hangups module.

I wrote the Slack part first because it was easier and I had some prior experience with it. The official SlackHQ implementation for Python is what I would call synchronous. I had to write my own do_one_loop() function that polled the Slack API to check for any incoming messages over their RealTime Messaging protocol.

When I wanted to attach the Google Hangouts integration, I ran into the same problem with trying to use an event loop on a program that doesn't already use it: Python 3's asyncio. There are several Google Hangouts bots already written for Python, and most of them only do one bot, which I didn't want (I want my one Python script to connect multiple bots to multiple platforms simultaneously). To use the Hangouts module I have to let asyncio take over control of the script and handle the main loop on its own, which would result in the Slack bot being starved out, as the Slack bot's loop to poll for events would never get a chance to run anymore.

Another strike against event loop frameworks being bolted on top of languages that didn't have them built in.

So, the second chatbot I wrote yesterday, I wrote in Go. I called this one Scarecrow, and this one is also a Slack bot for now but I'm more optimistic about this one due to the Go language itself. In addition to Slack it has a local command line interface that can chat locally with you simultaneously to having one or more Slack bots running.

Optimism for Golang

Something I like so far about the Go language is that it has a system for running concurrent tasks built in. They're called Goroutines and they're a first-class citizen in the language, and they should render ALL event loop frameworks obsolete.

On my near-term TODO list for the Scarecrow chatbot is to add XMPP support. I think it won't matter how the XMPP module is written; in the worst case scenario I can go run the XMPP code and isolate it from the entire rest of my program without any issue.

Opinion: Libraries should NOT use event frameworks

This is basically the point of this rant. If you're writing a library, such as an XMPP client or a Google Hangouts client, you absolutely should not use an event-loop framework in your code. Hear me out.

If you wrote a Hangouts module and you wrote it synchronously as mentioned above, with something like a start() and/or do_one_loop() function, you give the end developer of your module the best of all worlds. If I just want to write the simplest chatbot I can, one that uses only your module and nobody else's, I can do that very easily. And if I have a more complex program to write, I can choose an event framework that I personally like and I can wrap your module inside one of my coroutines on my own.

Like, suppose you have a synchronous module and this is my pseudocode written in Go:

func Example() {
    // I want to use a synchronous module... I can just make my own goroutine to wrap it.
    go YourModuleWrapper()
}

func YourModuleWrapper() {
    for {
        your_module_instance.do_one_loop()
    }
}

This way, if I already have an existing codebase and I find your module and I want to use it, I can. I don't have to see what crazy event loop framework it uses, I don't have to rearchitect the very core of my application to fit the use case of that framework, I don't have to deal with trying to get two competing frameworks to coexist together... I can just use it. If my use case requires a high level of concurrency, I can choose whatever framework I want and I can wrap your code to suit my use case. If I'm using a graphical framework like GTK+ I can wrap your module in that framework's event loop on my own, without having to deal with the headaches of getting different event loops to get along with each other.

Don't use event loop frameworks.

Event loops, if needed at all, should be at the sole discretion of application writers, people who are writing the actual code that runs somewhere and serves a purpose. NOT the library writers. Libraries should not build themselves around a particular event framework.

</rant>

Comments

There are 0 comments on this page.

Add a Comment

Your name:
Your Email:
Message:
Comments can be formatted with Markdown, and you can use
emoticons in your comment.

If you can see this, don't touch the following fields.



Kirsle
Channels
Creativity
Software
Web Tools
Subdomains
Miscellany
Links


Fan Club