2

Does anyone know of an event loop library (or bindings) available for Python 3? It's okay if it only does UNIX systems, though I would prefer something that does Windows as well.

ETA: I realize that it is not terribly difficult to write an event loop system. However, I don't want to reinvent the wheel (we are still encouraging not doing so these days, right? ;-))

This is planned for a server application, so obviously I'd want something that isn't tied to a GUI widget toolkit or something.

If the answer is "Nope, there isn't one" (probably; I sure as heck can't find one) then I will most likely create a binding for Python 3 for libev.

Michael Trausch
  • 3,187
  • 1
  • 21
  • 29
  • Event loops are trivial. Do you have any requirements? – Lennart Regebro Jul 25 '11 at 07:20
  • High performance. Ideally something like libev; I realize that I can write my own fd-based event loop for the system, but I'd rather not reinvent the wheel; there are several implementations. It just seems that very few of them have bindings for Python 3. – Michael Trausch Jul 25 '11 at 07:23
  • A simple `while True:` loop will be high performance. You can't get much higher, really. It only gets complicated if you want anything else, such as multithreading, or dispatching events over networks, etc, etc. Also you may have the desire to hook in system events, in which case you need support for that, and that's not trivial. For just a "High performance event loop" all you need is a `while True:`. – Lennart Regebro Jul 25 '11 at 07:50
  • 1
    Well, no; constantly polling like that will spin the CPU, and on a multiprocessor system that is very poor behavior indeed. `while True:` and setting up a select is fine, for very simple things, but even doing that in Python directly is going to be slower than using libev, which has Python 3.2 bindings (see below), because Python does a lot of "hidden magic" when you call `select`, for example. – Michael Trausch Jul 25 '11 at 08:06
  • But won't using a C-library mean you have to do a lot of conversion between C-types and Python types? – Lennart Regebro Jul 25 '11 at 10:53
  • It shouldn't as mostly the only thing going back and forth are integers, which require minimal (if any) marshaling. That'd be a premature optimization at this point; if a bottleneck _does_ occur, then I'd wind up moving some of the event handling out of Python and into C such that I could reduce the C/Python interaction to nothing more than simple function calls. – Michael Trausch Jul 25 '11 at 17:20
  • @LennartRegebro let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1803/discussion-between-michael-trausch-and-lennart-regebro) – Michael Trausch Jul 25 '11 at 17:20
  • Integers also require marshalling, but it should not be overly heavy, so if you can restrict it to integers... That is some heavy restriction in Python, though... – Lennart Regebro Jul 25 '11 at 17:28

2 Answers2

6

I suggest something like:

while True:
    while queue:
        queue.pop()()

For that to work, however, you need to have the event system put callable events onto the queue.

(If you are more interested in a specific binding to a specific framework, such as GTK, Qt, WxWidgets, NCurses, Cocoa, Winforms, whatever, then say that!).

Arafangion
  • 11,517
  • 1
  • 40
  • 72
  • Not really a specific one, just one that has been around for a while, has excellent performance, and supports all the normal bells-and-whistles (file descriptors, signals, etc.); I may just create a wrapper for python3 for libev if there is nothing else for it. I certainly do not want to reinvent the wheel for the ten millionth time. – Michael Trausch Jul 25 '11 at 07:25
  • For that specific event system, I might investigate http://code.google.com/p/pyev/ - Their change control system for that project seems to mention python3 from time to time. – Arafangion Jul 25 '11 at 07:41
  • Oh silly me. I saw the project; I somehow managed to miss that they have Python 3 support. Most excellent. That perhaps why I should not post questions in the wee hours of the morning...! Thanks! – Michael Trausch Jul 25 '11 at 07:45
5

libev is available for python as pyev module: http://code.google.com/p/pyev/