Re: why does socket.makefile require non-blocking mode?

Discussion in 'Python' started by Forest, Mar 31, 2008.

  1. Forest

    Forest Guest

    Bryan Olson wrote:

    >Looking at the code for the existing _fileobject's read method, it
    >will loose data it has already read if a socket.recv() call raises
    >an exception. The function keeps buffers in a local variable that
    >will be lost if an exception exits the scope. That much could be
    >fixed with a try...finally. Other methods have similar problems.


    Just as I thought. Thanks for being my extra set of eyes.

    >>I wanted to use file-like objects with socket timeouts, so I ended up
    >>writing my own replacement for socket._fileobject. I'd appreciate it
    >>if someone could either explain to my why my new class was unnecessary,
    >>or else encourage me to contribute it as a patch to the socket module.

    >
    >Sure, fix it.


    Yeah, I'll give it some more thought.

    >A harder problem is that it doesn't play nice with select().


    I guess you mean that since _fileobject.read() calls recv() multiple
    times, the second and later calls might block even if select() said the
    socket was
    readable. That should be fixable by catching EAGAIN / EWOULDBLOCK and
    returning early, shouldn't it? This idea seems consistent with the normal
    file object's read() method docs, which say, "in non-blocking mode, less
    data than what was requested may be returned." Making write() and flush()
    friendly in non-blocking mode might be at little trickier.
    Forest, Mar 31, 2008
    #1
    1. Advertising

  2. Forest

    greg Guest

    Forest wrote:
    > I guess you mean that since _fileobject.read() calls recv() multiple
    > times, the second and later calls might block even if select() said the
    > socket was
    > readable.


    The main problem is buffering. The select() may block on
    the socket even though the file object has data in its
    buffer waiting to be read.

    When using select(), you really need to deal with the
    socket directly, with no buffering in the way.

    --
    Greg
    greg, Apr 6, 2008
    #2
    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. Tim Black
    Replies:
    1
    Views:
    1,091
    Alan Kennedy
    Aug 3, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,805
    Smokey Grindel
    Dec 2, 2006
  3. Forest
    Replies:
    1
    Views:
    634
    Bryan Olson
    Mar 29, 2008
  4. Serge Savoie
    Replies:
    4
    Views:
    262
    Serge Savoie
    Oct 1, 2008
  5. Antoon Pardon
    Replies:
    0
    Views:
    133
    Antoon Pardon
    Jan 9, 2013
Loading...

Share This Page