non-blocking sockets

Michael Gilfix mgilfix at eecs.tufts.edu
Tue Apr 23 11:36:03 EDT 2002


  You need to use the select module to poll the socket. The select
module allows you to specify how long you want your timeout (in this
case, you probably want a timeout of 0, which is simply a poll) and
the module tells you which sockets are waiting for reading/writing.

  import select
  read_sock = <my readable socket>
  w_ready, r_ready, e_ready = select.select ([ ], [ read_sock ], [ ], 0)

  if read_sock in r_ready:
    # Process socket

  ... will probably do what you want.

  Check out the library ref docs for select at:
http://www.python.org/doc/current/lib/module-select.html

                 -- Mike

On Tue, Apr 23 @ 15:00, so very tired wrote:
> When trying to read from a socket, I don't want it hanging, so I set it to
> non-blocking by calling
> 
> setblocking(0)
> 
> but then when I try to read from it and there is no data in the socket, I
> get an error. I tried this in both windows and linux and in both I get an
> error and the program stops. It says there's an exception but it doesn't
> say the name of the exception so I can't even try to catch.
> Does anyone know how I can check if there's data coming from a socket
> connection without hanging or having the program barf on me?
> Thanks.
> mRiaz
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
`-> (g0riaman)

-- 
Michael Gilfix
mgilfix at eecs.tufts.edu

For my gpg public key:
http://www.eecs.tufts.edu/~mgilfix/contact.html





More information about the Python-list mailing list