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: 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 Date: Thu, 03 Feb 2011 10:53:01 -0800 From: Stephen Hansen MIME-Version: 1.0 To: Dwayne Blind Subject: Re: socket.rcv timeout while-loop References: <4D4AEE18.5050301@ixokai.io> In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigB122AD3130A8E86217139A46 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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 appl= y > 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 =3D 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 =3D 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. --=20 Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/ --------------enigB122AD3130A8E86217139A46 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.10 (Darwin) iQEcBAEBAgAGBQJNSvmOAAoJEKcbwptVWx/l6gEH/0m0M4e/9QGl6+ZIBWUXqcdF AHmHwRIqCICp322egMQ5HNx+g7vgKenqzXXreQx9tsgje/JOPNtHj5kN6fnDUhBD vJHNY1VKi4NxNW8CuibVHy97WII1y6e3DETMrZJr2b4ZYoyLJ0HhTKF4MzB0iUoq QqP1qWQQ3TtQah3KOfD4s0ky/T9Gr0aIlZeLINmOZI/s8j3+xtghH4ydM6i8zST/ +pbamoefoiJlxIMG8f4uAFcmGUlC0UJw3J2dX8zbi2JYOHa17QP6cDjc2nRdvCr3 VFPHg8Va34qOO1Lzl3PVwUt2CnEaimBkqJNxLAtBPARfpWM+Aq+OgT/MzjU7Vg0= =HAdz -----END PGP SIGNATURE----- --------------enigB122AD3130A8E86217139A46--