Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55943

Re: socket.rcv timeout while-loop

Path csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <me+list/python@ixokai.io>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'socket': 0.05; 'subject:while': 0.05; 'exception,': 0.07; 'method,': 0.07; 'roll': 0.07; 'run,': 0.07; 'also:': 0.09; 'content- type:multipart/signed': 0.09; 'decorator': 0.09; 'finally:': 0.09; 'received:209.85.213': 0.09; 'am,': 0.13; 'wrote:': 0.14; 'def': 0.14; 'argument': 0.15; 'pointed': 0.15; '(at)': 0.16; '(dot)': 0.16; 'content-type:application/pgp-signature': 0.16; 'contextlib': 0.16; 'filename:fname piece:asc': 0.16; 'filename:fname piece:signature': 0.16; 'filename:fname:signature.asc': 0.16; 's.recv(1024)': 0.16; 'sock': 0.16; 'socket.': 0.16; 'throws': 0.16; 'timeout': 0.16; 'cc:no real name:2**0': 0.17; 'yield': 0.18; 'cc:2**0': 0.21; 'personally,': 0.23; 'header:In-Reply-To:1': 0.23; 'cc:addr :python-list': 0.24; 'thanks': 0.30; 'clear,': 0.30; 'executed': 0.30; 'hansen': 0.30; 'skip:@ 10': 0.30; 'cc:addr:python.org': 0.30; '---': 0.31; 'import': 0.31; '...': 0.31; 'reset': 0.32; 'does': 0.33; 'try:': 0.34; 'change': 0.34; 'apply': 0.36; 'else,': 0.36; 'received:24': 0.36; 'blog:': 0.37; "i'd": 0.38; 'skip:s 20': 0.38; 'received:209.85': 0.38; 'received:google.com': 0.38; 'stephen': 0.38; 'end': 0.38; 'setting': 0.38; 'subject:: ': 0.39; 'your': 0.61; 'answer.': 0.64; 'prefer': 0.65; 'mail:': 0.67; 'reply-to:no real name:2**0': 0.72; 'header:Reply-To:1': 0.72; 'manager.': 0.76; "'with'": 0.84; 'url:io': 0.93
Sender Ixokai <ixokai@ixokai.io>
Date Thu, 03 Feb 2011 10:53:01 -0800
From Stephen Hansen <me+list/python@ixokai.io>
MIME-Version 1.0
To Dwayne Blind <dwayneblind@gmail.com>
Subject Re: socket.rcv timeout while-loop
References <AANLkTimSZYkOHkYtdy1QoT7GOBBXiLrORQvvUgfASvjD@mail.gmail.com> <4D4AEE18.5050301@ixokai.io> <AANLkTim1UM0f59rkMB8Nq5Sz7xTakXt6PN9qJvZKzBp-@mail.gmail.com>
In-Reply-To <AANLkTim1UM0f59rkMB8Nq5Sz7xTakXt6PN9qJvZKzBp-@mail.gmail.com>
X-Enigmail-Version 1.1.1
OpenPGP id=555B1FE5; url=http://id.ixokai.io/pub.asc
Content-Type multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigB122AD3130A8E86217139A46"
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To me+list/python@ixokai.io
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1629.1296759197.6505.python-list@python.org> (permalink)
Lines 78
NNTP-Posting-Host 82.94.164.166
X-Trace 1296759197 news.xs4all.nl 34849 [::ffff:82.94.164.166]:34768
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:55943

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 2/3/11 10:13 AM, Dwayne Blind wrote:
> Thanks for your answer. I don't want to reset my socket. I want to apply
> the timeout to the rcv method only.

Setting the timeout does not "reset [your] socket", I don't think. And I
get that you want to only timeout recv... that's why I pointed out its a
socket method, not an argument to recv. If you don't want it to apply to
everything else, you just have to be sure to change it back after recv.

Just:
  timeout = s.gettimeout()
  s.settimeout(3)
  s.recv(1024)
  s.settimeout(timeout)

Personally, I'd prefer to do:

with timeout(s, 3):
    s.recv(1024)

That's a lot more clear, and I'd roll this context manager to accomplish it:

--- start

from contextlib import contextmanager

@contextmanager
def timeout(sock, timeout):
    old_timeout = sock.gettimeout()
    sock.settimeout(timeout)
    try:
        yield sock
    finally:
        sock.settimeout(old_timeout)

--- end

The contextmanager decorator is an easy/quick way of making a context
manager. Everything up until the yield is executed before the 'with'
block is run, and everything after the yield is executed after the
'with' block concludes.

If the with block throws an exception, it'll be catchable at the yield
point.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: socket.rcv timeout while-loop Stephen Hansen <me+list/python@ixokai.io> - 2011-02-03 10:53 -0800

csiph-web