socket timeouts

Discussion in 'Python' started by =?ISO-8859-2?Q?Nagy_L=E1szl=F3_Zsolt?=, Oct 9, 2003.

  1. Hi Python Gurus!

    Here is a method I used before to receive data over a socket (with
    Python 2.2):

    SELECT_GRANULARITY = 0.1 # 0.1 seconds

    def readdata(self,length,timeout):
    res = ''
    remain = length
    lapsed = 0.0
    while (remain > 0) and (lapsed < timeout):
    i,o,e = select.select([self.socket],[],[],SELECT_GRANULARITY)
    lapsed = lapsed + SELECT_GRANULARITY
    if i:
    res = res + self.socket.recv(remain)
    remain = length - len(res)
    if remain > 0:
    raise TimeOutError
    else:
    return res

    I used a very similar code to send data over a socket. Now in Python
    2.3, we have
    the wonderful settimeout() method. Here is my problem. Suppose that the
    other
    side (sender) is rather slow. I want to receive 1MB data in 20 seconds
    (at most). Probably
    the socket socket.recv() call will not return all data after the first
    call. So my old method
    is continuously trying to receive data until the timeout arrives (or it
    will return sooner if all data
    was received). This worked flawlessly with Python 2.2. With the new api,
    it is possible to
    use settimeout() instead of the select.select() call. However, recv()
    will not return
    all the 1MB data after the first call either. No matter what was the
    timeout, it will give up
    receiving when the input buffer becomes empty. So I still need to
    receive data in a while loop.
    What are the benefits and drawback of this solution with the new
    settimeout() method?

    I read the library reference documentation about compatibility. There is
    not a word about
    what platforms support the settimeout() method so I suppose all
    platforms support this. As
    opposed to select.select which is not available on every OS. There is a
    note for the select module::

    "This module provides access to the select() and poll() functions
    available in most operating systems."

    A proposal: It would be nice to have a recvall() method with a length
    parameter. Suggestions,
    comments are welcome.

    Thank you in advance,

    Laci 1.0
    =?ISO-8859-2?Q?Nagy_L=E1szl=F3_Zsolt?=, Oct 9, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmFzb24=?=

    forms authentication timeouts and session timeouts

    =?Utf-8?B?SmFzb24=?=, Jun 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    438
    =?Utf-8?B?SmFzb24=?=
    Jun 22, 2004
  2. =?ISO-8859-2?Q?Nagy_L=E1szl=F3_Zsolt?=

    Socket timeouts

    =?ISO-8859-2?Q?Nagy_L=E1szl=F3_Zsolt?=, Oct 3, 2003, in forum: Python
    Replies:
    0
    Views:
    276
    =?ISO-8859-2?Q?Nagy_L=E1szl=F3_Zsolt?=
    Oct 3, 2003
  3. John Nagle
    Replies:
    1
    Views:
    356
    Steve Holden
    Apr 24, 2007
  4. John Nagle
    Replies:
    0
    Views:
    255
    John Nagle
    Sep 30, 2007
  5. Cameron Simpson

    Re: Question on Socket Timeouts

    Cameron Simpson, Nov 19, 2012, in forum: Python
    Replies:
    0
    Views:
    150
    Cameron Simpson
    Nov 19, 2012
Loading...

Share This Page