Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > sockets,threads and interupts

Reply
Thread Tools

sockets,threads and interupts

 
 
loial
Guest
Posts: n/a
 
      09-04-2012
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.


 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      09-04-2012
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.
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      09-04-2012
On 2012-09-04, MRAB <> 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
 
Reply With Quote
 
Ramchandra Apte
Guest
Posts: n/a
 
      09-05-2012
On Tuesday, 4 September 2012 23:41:13 UTC+5:30, Grant Edwards wrote:
> On 2012-09-04, MRAB <> 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


We could just use a "with" statement. Neater, easier.
 
Reply With Quote
 
Dieter Maurer
Guest
Posts: n/a
 
      09-05-2012
loial <> writes:

> 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.


The operating system should close all sockets automatically when
the process dies. Thus, if closing alone is sufficient...

 
Reply With Quote
 
Ramchandra Apte
Guest
Posts: n/a
 
      09-05-2012
On Wednesday, 5 September 2012 11:26:16 UTC+5:30, Dieter Maurer wrote:
> loial <> writes:
>
>
>
> > 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.

>
>
>
> The operating system should close all sockets automatically when
>
> the process dies. Thus, if closing alone is sufficient...


At least on Linux, if you kill a process using sockets, it takes about 10 seconds for socket to be closed. A program should try to close all resources. OS'es may take a long time to close a unclosed socket automatically.
 
Reply With Quote
 
Ramchandra Apte
Guest
Posts: n/a
 
      09-05-2012
On Wednesday, 5 September 2012 11:26:16 UTC+5:30, Dieter Maurer wrote:
> loial <> writes:
>
>
>
> > 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.

>
>
>
> The operating system should close all sockets automatically when
>
> the process dies. Thus, if closing alone is sufficient...


At least on Linux, if you kill a process using sockets, it takes about 10 seconds for socket to be closed. A program should try to close all resources. OS'es may take a long time to close a unclosed socket automatically.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      09-05-2012
On Wed, Sep 5, 2012 at 10:54 PM, Ramchandra Apte <> wrote:
> At least on Linux, if you kill a process using sockets, it takes about 10 seconds for socket to be closed. A program should try to close all resources. OS'es may take a long time to close a unclosed socket automatically.


Err, that's not my experience. When a process terminates, its
resources are released promptly.

ChrisA
 
Reply With Quote
 
Ramchandra Apte
Guest
Posts: n/a
 
      09-05-2012
On Wednesday, 5 September 2012 18:34:32 UTC+5:30, Chris Angelico wrote:
> On Wed, Sep 5, 2012 at 10:54 PM, Ramchandra Apte <> wrote:
>
> > At least on Linux, if you kill a process using sockets, it takes about 10 seconds for socket to be closed. A program should try to close all resources. OS'es may take a long time to close a unclosed socket automatically.

>
>
>
> Err, that's not my experience. When a process terminates, its
>
> resources are released promptly.


It is not guaranteed so a program shouldn't presume.
>
>
>
> ChrisA


 
Reply With Quote
 
Ramchandra Apte
Guest
Posts: n/a
 
      09-05-2012
On Wednesday, 5 September 2012 18:34:32 UTC+5:30, Chris Angelico wrote:
> On Wed, Sep 5, 2012 at 10:54 PM, Ramchandra Apte <> wrote:
>
> > At least on Linux, if you kill a process using sockets, it takes about 10 seconds for socket to be closed. A program should try to close all resources. OS'es may take a long time to close a unclosed socket automatically.

>
>
>
> Err, that's not my experience. When a process terminates, its
>
> resources are released promptly.


It is not guaranteed so a program shouldn't presume.
>
>
>
> ChrisA


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM
Signals and keyboard interupts Martin Miller Python 0 10-27-2005 08:48 PM
Hard Drive Problem - "Interupts Time Placeholder"?!? JM Computer Support 7 03-06-2005 06:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57