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.

8
  • Event loops are trivial. Do you have any requirements? Commented Jul 25, 2011 at 7: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. Commented Jul 25, 2011 at 7: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:. Commented Jul 25, 2011 at 7: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. Commented Jul 25, 2011 at 8:06
  • But won't using a C-library mean you have to do a lot of conversion between C-types and Python types? Commented Jul 25, 2011 at 10:53

2 Answers 2

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!).

Sign up to request clarification or add additional context in comments.

3 Comments

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.
For that specific event system, I might investigate code.google.com/p/pyev - Their change control system for that project seems to mention python3 from time to time.
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!
5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.