Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Cameron Simpson Newsgroups: comp.lang.python Subject: Re: cross platform alternative for signal.SIGALRM? Date: Thu, 12 Nov 2015 16:20:46 +1100 Lines: 53 Message-ID: References: Reply-To: python-list@python.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed X-Trace: news.uni-berlin.de s9edIgZgP6P2RoVvWI5bcAAx8A92ZHV3RoLteZzG/ugQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:skip:s 10': 0.05; ':-(': 0.07; 'chunk': 0.07; 'socket': 0.07; 'cc:addr:python-list': 0.09; 'timeout': 0.09; 'python': 0.10; 'def': 0.13; 'suggest': 0.15; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'rewriting': 0.16; 'simpson': 0.16; 'subject:alternative': 0.16; 'timeout,': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'try:': 0.18; 'windows': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'cheers,': 0.22; 'cc:no real name:2**0': 0.22; '(or': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'linux': 0.26; 'complain': 0.29; 'perl': 0.29; 'code:': 0.29; 'raise': 0.29; 'skip:s 30': 0.31; 'run': 0.33; 'url:python': 0.33; 'windows.': 0.33; 'file': 0.34; 'except': 0.34; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'skip:v 20': 0.38; 'skip:s 40': 0.38; 'some': 0.40; 'avoid': 0.61; 'show': 0.62; 'cameron': 0.66; 'header:Reply-To:1': 0.67; 'reply- to:no real name:2**0': 0.71; 'received:61': 0.72; '>with': 0.84; 'confusing': 0.84; 'reply-to:addr:python.org': 0.84; 'rick': 0.93 X-Authentication-Info: Submitted using ID cskk@bigpond.com X-Authority-Analysis: v=2.0 cv=JY8+XD2V c=1 sm=1 a=w8UANqRlQPdik3ney68ylg==:17 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=8AHkEIZyAAAA:8 a=qtqOOiqGOCEA:10 a=K8_jpOyAAAAA:8 a=7HZYEeCsNOFu573pOl8A:9 a=CjuIK1q_8ugA:10 a=NOGS43Lr1mwA:10 a=w8UANqRlQPdik3ney68ylg==:117 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98672 On 11Nov2015 16:16, Ulli Horlacher wrote: >I am rewriting a Perl program into Python (2.7). >It must run on Linux and Windows. >With Linux I have no problems, but Windows... :-( > >The current show stopper is signal.SIGALRM which is not available on >Windows: > > File "fexit.py", line 674, in formdata_post > signal.signal(signal.SIGALRM,timeout_handler) > AttributeError: 'module' object has no attribute 'SIGALRM' > > https://docs.python.org/2/library/signal.html > > signal.alarm(time) (...) Availability: Unix. > >Perl for Windows has had SIGALRM support (or some kind of emulation). > >Ok, I have to redesign this part of my code: > > def timeout_handler(sig,frame): > raise ValueError("timeout!") > > signal.signal(signal.SIGALRM,timeout_handler) > > while True: > chunk = fileo.read(bs) > sock.sendall(chunk) > (...) > >What is the best practise for a cross platform timeout handler? I suggest you look at the socket.settimeout function. Avoid SIGALRM altogether. Then (untested): import socket ... socket.settimeout(timeout_in_seconds) ... while True: ... chunk = fileo.read(bs) try: sock.sendall(chunk) except socket.timeout as e: ... complain about timeout, reciting "e" in the message ... Cheers, Cameron Simpson I think you're confusing "recognizing" and "understanding" with "caring". The net is cruel, sometimes, but always fair. - Rick Gordon