Path: csiph.com!usenet.pasdenom.info!news.albasani.net!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!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; 'exception': 0.03; 'explicitly': 0.04; 'handler': 0.04; 'interpreter': 0.04; 'subject:Python': 0.05; 'memory.': 0.05; 'exit': 0.07; 'ok.': 0.07; 'python': 0.09; 'handlers': 0.09; 'non-blocking': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sep': 0.09; 'thread': 0.11; 'suggest': 0.11; 'sat,': 0.15; 'corrupt': 0.16; 'elsewhere.': 0.16; 'least,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subprocess': 0.16; 'thread.': 0.16; 'saying': 0.18; '(or': 0.18; 'causing': 0.20; 'sort': 0.21; 'all,': 0.21; 'ctypes': 0.22; 'terminate': 0.22; 'raise': 0.24; 'leave': 0.26; 'header:X-Complaints-To:1': 0.28; 'trouble': 0.28; 'run': 0.28; 'facing': 0.29; 'i/o': 0.29; 'wrap': 0.29; 'case,': 0.29; 'probably': 0.29; 'maybe': 0.29; 'code': 0.31; 'point': 0.31; 'could': 0.32; 'url:home': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'likely': 0.33; 'operations': 0.33; 'another': 0.33; "can't": 0.34; 'so,': 0.35; "won't": 0.35; 'received:org': 0.36; 'but': 0.36; 'anything': 0.36; 'test': 0.36; 'should': 0.36; 'uses': 0.37; '(for': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'help': 0.40; 'truly': 0.62; 'between': 0.63; 'different': 0.63; 'hear': 0.63; 'more': 0.63; 'else.': 0.65; 'state,': 0.65; 'potentially': 0.66; 'saving': 0.72; 'me).': 0.84; 'taken.': 0.84; 'approach.': 0.91; 'dennis': 0.91; 'safer': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: QThread.terminate in Python 3 Date: Sat, 29 Sep 2012 14:05:48 -0400 Organization: > Bestiaria Support Staff < References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: adsl-76-249-28-56.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348941943 news.xs4all.nl 6865 [2001:888:2000:d::a6]:38179 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30518 On Sat, 29 Sep 2012 06:34:00 +0430, Lee Harr declaimed the following in gmane.comp.python.general: > > Ok. Point taken. > > What I hear you saying is that once I use .terminate anything > following that is indeterminate. It might work on my system > and nowhere else. Even though it was working for me before, > it was likely causing trouble elsewhere. > Even changing a line of code could potentially cause a problem (if the terminate action takes place between a different set of Python op-codes). > > So, I need another approach. > > The problem I am facing is that I want to run arbitrary > (user-supplied) code in the other thread and need to be > able to stop it at any time. > The cleanest, that I can visualize (though I can't suggest implementation), is probably to run as an independent subprocess (or maybe via multiprocess), with some sort of non-blocking (polled?) I/O for results. You should be able to use OS-level operations to terminate the independent process and ensure the system reclaims all associated memory. > It uses ctypes to raise an exception in the other thread. > For the simple test case, at least, it works (for me). > > Is this any safer or more reliable than .terminate ? > Out of my experience; but if the exception is truly in the other thread it should be a clean exit -- since the exception detection code is part of the Python interpreter logic, it should be saving clean states (after all, it it could leave a corrupt state, having exception handlers won't help in recovery). Even better would be to wrap the thread operation with an exception handler in which you can explicitly perform any clean-up (or, at least, log the situation). -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/