Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > 9d2e976f12e8ff1c53133a0ee8246441 > files > 13

python-xlib-doc-0.15-6.mga4.noarch.rpm

<HTML>
<HEAD>
<!-- Created by texi2html 1.56k from ../src/python-xlib.texi on 22 October 2013 -->

<TITLE>The Python X Library - Getting Events</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="python-xlib_1.html">first</A>, <A HREF="python-xlib_10.html">previous</A>, <A HREF="python-xlib_12.html">next</A>, <A HREF="python-xlib_25.html">last</A> section, <A HREF="python-xlib_toc.html">table of contents</A>.
<P><HR><P>


<H2><A NAME="SEC10" HREF="python-xlib_toc.html#TOC10">Getting Events</A></H2>

<P>
Events can be sent at any time, not necessarily when the client is ready
to recieve an event. Therefore they must be stored temporarily from that
they are read from the network until the client is ready to handle them.
Read but unhandled events are stored on an event queue in the Display
object.  There are two functions to access this queue:


<P>
<DL>
<DT><U>Method:</U> Display <B>next_event</B> <I>( )</I>
<DD><A NAME="IDX6"></A>


<P>
Return the next event in the event queue.  If the event queue is empty,
block until an event is read from the network, and return that one.


</DL>

<P>
<DL>
<DT><U>Method:</U> Display <B>pending_events</B> <I>( )</I>
<DD><A NAME="IDX7"></A>


<P>
Return the number of events which can be returned without blocking.


</DL>

<P>
A trivial event loop would simply loop infinitely, waiting for an
event and then handling it.  It could look like this:



<PRE>
while 1:
   event = disp.next_event()
   handle_event(event)
</PRE>

<P>
However, most applications need more control, e.g. to simultaneously
handle a network connection or at regular intervals schedule timeouts.
The module <CODE>select</CODE> is often used for this.  <CODE>Display</CODE> objects
can be used with <CODE>select</CODE>, since they have the required
<CODE>fileno()</CODE> method.  When <CODE>select</CODE> indicates that a
<CODE>Display</CODE> object is ready for reading, that means that the server
has sent some data to the client.  That alone doesn't guarantee that an
entire event has arrived, so one must first use <CODE>pending_events()</CODE>
to make sure that <CODE>next_event()</CODE> will return without blocking.  A
simple event loop which waits for events or a one-second timeout looks
like this:



<PRE>
while 1:
    # Wait for display to send something, or a timeout of one second
    readable, w, e = select.select([disp], [], [], 1)

    # if no files are ready to be read, it's an timeout
    if not readable:
        handle_timeout()

    # if display is readable, handle as many events as have been recieved
    elif disp in readable:
        i = disp.pending_events()
        while i &#62; 0:
            event = disp.next_event()
            handle_event(event)
            i = i - 1

    # loop around to wait for more things to happen
</PRE>

<P><HR><P>
Go to the <A HREF="python-xlib_1.html">first</A>, <A HREF="python-xlib_10.html">previous</A>, <A HREF="python-xlib_12.html">next</A>, <A HREF="python-xlib_25.html">last</A> section, <A HREF="python-xlib_toc.html">table of contents</A>.
</BODY>
</HTML>