Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28403 > unrolled thread

sockets,threads and interupts

Started byloial <jldunn2000@gmail.com>
First post2012-09-04 08:26 -0700
Last post2012-09-05 09:02 -0700
Articles 14 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  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

#28403 — sockets,threads and interupts

Fromloial <jldunn2000@gmail.com>
Date2012-09-04 08:26 -0700
Subjectsockets,threads and interupts
Message-ID<4d1ffb61-38b6-42fb-9426-c1c7cb7038a0@googlegroups.com>
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.

[toc] | [next] | [standalone]


#28406

FromMRAB <python@mrabarnett.plus.com>
Date2012-09-04 18:06 +0100
Message-ID<mailman.181.1346778382.27098.python-list@python.org>
In reply to#28403
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.

[toc] | [prev] | [next] | [standalone]


#28412

FromGrant Edwards <invalid@invalid.invalid>
Date2012-09-04 18:11 +0000
Message-ID<k25g80$m5v$1@reader1.panix.com>
In reply to#28406
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            

[toc] | [prev] | [next] | [standalone]


#28446

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-04 20:43 -0700
Message-ID<340d794c-b7e8-49e1-9449-5e5314baa98c@googlegroups.com>
In reply to#28412
On Tuesday, 4 September 2012 23:41:13 UTC+5:30, Grant Edwards  wrote:
> 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

We could just use a "with" statement. Neater, easier.

[toc] | [prev] | [next] | [standalone]


#28453

FromDieter Maurer <dieter@handshake.de>
Date2012-09-05 07:56 +0200
Message-ID<mailman.210.1346824575.27098.python-list@python.org>
In reply to#28403
loial <jldunn2000@gmail.com> 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...

[toc] | [prev] | [next] | [standalone]


#28484

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-05 05:54 -0700
Message-ID<4baa457c-3408-46dd-a98b-ae7e097ac5e5@googlegroups.com>
In reply to#28453
On Wednesday, 5 September 2012 11:26:16 UTC+5:30, Dieter Maurer  wrote:
> loial <jldunn2000@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#28489

FromChris Angelico <rosuav@gmail.com>
Date2012-09-05 23:04 +1000
Message-ID<mailman.232.1346850266.27098.python-list@python.org>
In reply to#28484
On Wed, Sep 5, 2012 at 10:54 PM, Ramchandra Apte <maniandram01@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#28511

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-05 08:59 -0700
Message-ID<c08969fb-7b4f-419f-8c21-fc783311b64f@googlegroups.com>
In reply to#28489
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 <maniandram01@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#28512

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-05 08:59 -0700
Message-ID<mailman.248.1346860748.27098.python-list@python.org>
In reply to#28489
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 <maniandram01@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#28513

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-05 09:01 -0700
Message-ID<2ae930cf-0af2-4526-a15a-667a649633c4@googlegroups.com>
In reply to#28512
On Wednesday, 5 September 2012 21:29:12 UTC+5:30, Ramchandra Apte  wrote:
> 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 <maniandram01@gmail.com> 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

oops forgot my signature
---
Bragging rights (I belong an exclusive community of banned people): got banned on SO and #python-offtopic
Projects:http://code.google.com/p/py2c/ and http://code.google.com/p/uniqos (name may be changed to PyOS)

[toc] | [prev] | [next] | [standalone]


#28514

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-05 09:01 -0700
Message-ID<mailman.249.1346860881.27098.python-list@python.org>
In reply to#28512
On Wednesday, 5 September 2012 21:29:12 UTC+5:30, Ramchandra Apte  wrote:
> 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 <maniandram01@gmail.com> 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

oops forgot my signature
---
Bragging rights (I belong an exclusive community of banned people): got banned on SO and #python-offtopic
Projects:http://code.google.com/p/py2c/ and http://code.google.com/p/uniqos (name may be changed to PyOS)

[toc] | [prev] | [next] | [standalone]


#28527

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-09-05 13:15 -0400
Message-ID<mailman.257.1346865314.27098.python-list@python.org>
In reply to#28484
On Wed, 5 Sep 2012 05:54:41 -0700 (PDT), Ramchandra Apte
<maniandram01@gmail.com> declaimed the following in
gmane.comp.python.general:


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

	Are you measuring the time for the socket to close, or the time
before the OS allows the socket (IP/Port) to be reused?
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#28485

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-05 05:54 -0700
Message-ID<mailman.231.1346849685.27098.python-list@python.org>
In reply to#28453
On Wednesday, 5 September 2012 11:26:16 UTC+5:30, Dieter Maurer  wrote:
> loial <jldunn2000@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#28516

FromBryan <bryanjugglercryptographer@yahoo.com>
Date2012-09-05 09:02 -0700
Message-ID<8aee6e43-82fd-42a4-aa9f-ff626145577d@ou2g2000pbc.googlegroups.com>
In reply to#28403
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?

You may have various threads waiting in blocking calls, and I don't
think there's a good way to alert them. Closing sockets that other
threads may be waiting on is "probably unwise" according to Linux man
page on close(2).

Do you really need to worry about it? If your process is being
forcibly terminated you probably cannot do anything better than the OS
will do by default.

-Bryan

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web