Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'socket': 0.05; 'clause': 0.07; 'run,': 0.07; 'python': 0.09; 'sockets': 0.09; 'thread,': 0.09; 'cleanly': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'subject:threads': 0.16; 'threaded': 0.16; 'trap': 0.16; 'wrote:': 0.17; 'work.': 0.23; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'there.': 0.28; 'received:192.168.1.3': 0.29; 'wrap': 0.29; 'class': 0.29; 'up.': 0.31; 'could': 0.32; 'to:addr :python-list': 0.33; 'monitor': 0.33; "won't": 0.35; 'does': 0.37; 'uses': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'chance': 0.61; 'close': 0.63; 'header :Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'guaranteed': 0.76; 'subject:,': 0.81; 'ports.': 0.84; 'reply- to:addr:python.org': 0.84; 'killed': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=W6e6pGqk c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=lR6CHUT36vYA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=FHbs8fUtD8AA:10 a=q1z7PMFHO0YA:10 a=qbg5HHIM_1kA:10 a=IKYNe_pnHKUA:10 a=v7ey3tHxI4MA:10 a=N4hpdHRZHgAA:10 a=sTOKQSFKWFoA:10 a=TFqcKDUXLT0A:10 a=RVSjMXkubmYA:10 a=haf4mBPXbV0A:10 a=YL8CwWjEfWsA:10 a=YsHCWR3NWtQA:10 a=7Cw1gAqGP1QwZJp7egwA:9 a=wPNLvfGTeEIA:10 a=P_PvJVi5qAcH1iYi:21 a=Y-z_TIqI_V8nCG2d:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Tue, 04 Sep 2012 18:06:17 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120824 Thunderbird/15.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: sockets,threads and interupts References: <4d1ffb61-38b6-42fb-9426-c1c7cb7038a0@googlegroups.com> In-Reply-To: <4d1ffb61-38b6-42fb-9426-c1c7cb7038a0@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 17 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346778382 news.xs4all.nl 6893 [2001:888:2000:d::a6]:48428 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28406 On 04/09/2012 16:26, loial wrote: > I have threaded python script that uses sockets to monitor network > ports. > > I want to ensure that the socket is closed cleanly in all > circumstances. This includes if the script is killed or interupted in > some other way. > > As I understand it signal only works in the main thread, so how can I > trap interupts in my threaded class and always ensure I close the > socket? Using KeyboardInterupt does not seem to work. > You could wrap it in try...finally. The 'finally' clause is guaranteed to be run, so you can close the sockets there. However, if the script is just killed, then it won't get the chance to tidy up.