Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: cross platform alternative for signal.SIGALRM? Date: Wed, 11 Nov 2015 20:37:34 -0500 Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de x7t3/eE0rwOsJ4xCZRzufwLkXL9bKT2yy95bN0gNH+jw== 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; 'subject:skip:s 10': 0.05; ':-(': 0.07; 'chunk': 0.07; 'wrapped': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'returns,': 0.09; 'timeout': 0.09; 'python': 0.10; 'jan': 0.11; 'def': 0.13; 'coroutines': 0.16; 'instance:': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'rewriting': 0.16; 'subject:alternative': 0.16; 'task.': 0.16; 'true:': 0.16; 'windows:': 0.16; 'wrote:': 0.16; 'looked': 0.16; 'windows': 0.20; 'pending': 0.20; 'am,': 0.23; '(or': 0.23; 'sets': 0.23; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X -Complaints-To:1': 0.26; 'linux': 0.26; 'cancel': 0.27; 'sequence': 0.27; 'yield': 0.27; 'perl': 0.29; 'code:': 0.29; 'objects': 0.29; 'raise': 0.29; 'task': 0.30; 'possibly': 0.32; 'run': 0.33; 'done,': 0.33; 'windows.': 0.33; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'skip:v 20': 0.38; 'skip:s 40': 0.38; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'show': 0.62; 'above,': 0.63; 'believe': 0.66; 'tasks.': 0.66; '2.7.': 0.84; '3.4': 0.84; 'received:fios.verizon.net': 0.91; 'task,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-173-59-124-74.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: 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:98662 On 11/11/2015 11:16 AM, Ulli Horlacher wrote: > I am rewriting a Perl program into Python (2.7). I recommend using 3.4+ if you possibly can. > 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: > 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? The cross-platform 3.4 asyncio module has some functions with timeouts. (3.5 has new 'async' syntac which supposedly makes it easier to use. I have not looked at this yet.) For instance: coroutine asyncio.wait(futures, *, loop=None, timeout=None, return_when=ALL_COMPLETED) Wait for the Futures and coroutine objects given by the sequence futures to complete. Coroutines will be wrapped in Tasks. Returns two sets of Future: (done, pending). ... Usage: done, pending = yield from asyncio.wait(fs) I believe the backport on pypi.python.org, called tulip, works on 2.7. In the example above, the read/send would be a task. Wait on the task, and when it returns, cancel the task if in pending. -- Terry Jan Reedy