How do I check for pending UDP input?

Colin Brown cbrown at
Fri Mar 14 00:18:49 CET 2003

"Peter Hansen" <peter at> wrote in message
news:3E70EC1D.557A4E4F at
> Colin Brown wrote:
> >
> > Hi, I am developing a multi-process application for theatrical lighting
> > using Python and communicate between processes via UDP.
> >
> > I need to keep some of the connections asynchronous and need to
> > be able to either get the last packet sent or return an empty string.
> >
> > My current code uses select with a small timeout to read in outstanding
> > packets until a timeout occurs returning the previous packet read if
> >
> > This is not elegant. Does anyone have experience with using SIOCINQ
> > and IOCTLS in Python to find if a datagram is pending?
> What's not elegant?  Select should return instantly when the socket
> on which you are waiting becomes "readable" (as opposed to how you
> appear to describe it, where it always waits until the timeout before
> returning).  This is the accepted way of doing this sort of thing,
> so you should probably redefine "elegant" and be happy. :-)
> -Peter

I probably need to clarify things a little. The UDP receiver is being used
in a number of different places. Some of the circuits have little traffic
others have more data than I can handle within that application and I am
wanting to just use the last packet sent. I know that "select" returns as
as "readable" data is present. The problem I have come across is that if I
do not empty the UDP receive buffer some of the comms slows right down
because I am not clearing the circuit faster than the data comes in. Using
"select" with a timeout means that I always have to read until I get an
string back upon a timeout to clear the buildup. Thus I am always incurring
the "timeout read". Yes, it works but if I could peek the receive buffer I
would not have to incur the timeout delay and thereby achieve proper
asynchronous operation.

Colin Brown

More information about the Python-list mailing list