Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28412
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: sockets,threads and interupts |
| Date | 2012-09-04 18:11 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <k25g80$m5v$1@reader1.panix.com> (permalink) |
| References | <4d1ffb61-38b6-42fb-9426-c1c7cb7038a0@googlegroups.com> <mailman.181.1346778382.27098.python-list@python.org> |
On 2012-09-04, MRAB <python@mrabarnett.plus.com> wrote:
> 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.
That depends on the signal used to "kill" the thread.
You can catch SIGTERM and SIGINT and clean up before exiting.
You can't catch SIGKILL, but sending a SIGKILL isn't considered polite
unless you've already tried SIGTERM/SIGINT and it didn't work.
--
Grant Edwards grant.b.edwards Yow! Are we on STRIKE yet?
at
gmail.com
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sockets,threads and interupts loial <jldunn2000@gmail.com> - 2012-09-04 08:26 -0700
Re: sockets,threads and interupts MRAB <python@mrabarnett.plus.com> - 2012-09-04 18:06 +0100
Re: sockets,threads and interupts Grant Edwards <invalid@invalid.invalid> - 2012-09-04 18:11 +0000
Re: sockets,threads and interupts Ramchandra Apte <maniandram01@gmail.com> - 2012-09-04 20:43 -0700
Re: sockets,threads and interupts Dieter Maurer <dieter@handshake.de> - 2012-09-05 07:56 +0200
Re: sockets,threads and interupts Ramchandra Apte <maniandram01@gmail.com> - 2012-09-05 05:54 -0700
Re: sockets,threads and interupts Chris Angelico <rosuav@gmail.com> - 2012-09-05 23:04 +1000
Re: sockets,threads and interupts Ramchandra Apte <maniandram01@gmail.com> - 2012-09-05 08:59 -0700
Re: sockets,threads and interupts Ramchandra Apte <maniandram01@gmail.com> - 2012-09-05 08:59 -0700
Re: sockets,threads and interupts Ramchandra Apte <maniandram01@gmail.com> - 2012-09-05 09:01 -0700
Re: sockets,threads and interupts Ramchandra Apte <maniandram01@gmail.com> - 2012-09-05 09:01 -0700
Re: sockets,threads and interupts Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-05 13:15 -0400
Re: sockets,threads and interupts Ramchandra Apte <maniandram01@gmail.com> - 2012-09-05 05:54 -0700
Re: sockets,threads and interupts Bryan <bryanjugglercryptographer@yahoo.com> - 2012-09-05 09:02 -0700
csiph-web