On May 7, 7:36 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > > Busy-looping like that is ugly and inefficient, even with the sleep > thrown in. > > Most GUI main loops _do_ use either select() or poll(). When Xt/GTK/ > Qt/etc have function like "gtk_add_input" which takes an fd that > you'll get notified about if it's written to while you're in the main > loop, that's just adding another fd to the select() loop. > > There are other ways to wait for events on an fd, but they tend to be > less portable. Depending on your Unix flavor, epoll, /dev/poll, > kqueues, kevent, queued realtime signals, or something else might be > available from the OS (but probably not from Python without futzing > with ctypes or writing an extension). If you want details, check > outhttp://www.kegel.com/c10k.html > > The alternatives usually aren't really faster unless you have hundreds > of connections, though--select/poll have major portability advantages, > so go with them unless you have a compelling reason.
I see where you are coming from. In a GUI or in a Web server most of the time is spent waiting from input, so a busy loop design would be a terrible design indeed. But I had another use case in mind. The code I posted is extracted from a batch script I have, called 'dbexplorer'. The script performs lots of queries in a database and find out "bad" data according to some criterium. So at each iteration there is a default action which is nontrivial; it becomes a time.sleep only at the end, when the batch has finished its job and it is waiting for input.In normal conditions most of the time is spent doing something, not waiting, so the busy loop design here is not so bad. Still I wanted to know what my alternatives were. And from this thread I gather the impression that actually the only portable alternative is using some kind of select, unless I want to use threads, and in that case the scheduler approach could be viable. Anyway, that C10K page is really an interesting resource, thanks for pointing it out! Michele Simionato -- http://mail.python.org/mailman/listinfo/python-list