How do I check for pending UDP input?

Colin Brown cbrown at metservice.com
Mon Mar 17 00:15:35 CET 2003


I revisited the problem over the weekend and carefully reread the
socket docs with an open mind. My solution to 'asynchronously'
reading the last available datagram if it exists is now as follows.

Set up socket to be non-blocking in class initialisation.

Reader method :-
    def recv(self,size=512):
        data = ''
        while 1:
            try:
                d = self.skt.recv(size)
                data = d
            except:
                return data

One could handle socket errors in the except statement if desired.

Colin Brown - Python aficionado
PyNZ


"Colin Brown" <cbrown at metservice.com> wrote in message
news:3e7145f5 at news.nz.asiaonline.net...
>
> Thanks
>
> Thinking outside the square, I like that. I will have to check that I
> am not getting thread conflicts with other software if I do it this way
> though.
>
> Colin
>
> "Dennis Lee Bieber" <wlfraed at ix.netcom.com> wrote in message
> news:t4n7k-eg3.ln1 at beastie.ix.netcom.com...
> > Colin Brown fed this fish to the penguins on Thursday 13 March 2003
> > 03:18 pm:
> >
> > >
> > > 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 but
> > > 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 soon
> > > 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 empty
> > > 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.
> > >
> >         I'm still not sure just what all is happening, but...
> >
> >         I'd suggest that you need to decouple the UDP handler from the
> main
> > application logic. Use a thread to read the UDP socket.
> >
> > {very rough psuedo-code}
> >
> > the UDP thread process (no initialization is shown)
> >
> > while 1:
> >         last_pkt = skt.recv()   #no need for select, as long as the recv
> is
> >         theLock.acquire()       #blocked, theGlobalPkt contains the last
> >         theGlobalPkt = last_pkt #packet received (or None)
> >         theLock.release()
> >
> > and the main application
> >
> > theGlobalPkt = None
> > #start the UDP thread here
> > while 1:
> >         theLock.acquire()
> >         pkt = theGlobalPkt      #get the most recent received packet
> >         theGlobalPkt = None     #assuming you don't want to process it
> again
> >         theLock.release()
> >         # do all the time consuming stuff while the UDP thread keeps
> >         # updating theGlobalPkt (which won't affect the local pkt
> >         # until you've finished with it and go back to check
theGlobalPkt
> >
> > --
> >  > ============================================================== <
> >  >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
> >  >      wulfraed at dm.net     |       Bestiaria Support Staff       <
> >  > ============================================================== <
> >  >        Bestiaria Home Page: http://www.beastie.dm.net/         <
> >  >            Home Page: http://www.dm.net/~wulfraed/             <
> >
>
>






More information about the Python-list mailing list