sockets,threads and interupts

Discussion in 'Python' started by loial, Sep 4, 2012.

  1. loial

    loial Guest

    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.
     
    loial, Sep 4, 2012
    #1
    1. Advertising

  2. loial

    MRAB Guest

    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.
     
    MRAB, Sep 4, 2012
    #2
    1. Advertising

  3. 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
     
    Grant Edwards, Sep 4, 2012
    #3
  4. 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.
     
    Ramchandra Apte, Sep 5, 2012
    #4
  5. 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...
     
    Dieter Maurer, Sep 5, 2012
    #5
  6. 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.
     
    Ramchandra Apte, Sep 5, 2012
    #6
  7. 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.
     
    Ramchandra Apte, Sep 5, 2012
    #7
  8. 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
     
    Chris Angelico, Sep 5, 2012
    #8
  9. 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
     
    Ramchandra Apte, Sep 5, 2012
    #9
  10. 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
     
    Ramchandra Apte, Sep 5, 2012
    #10
  11. 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 <> 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)
     
    Ramchandra Apte, Sep 5, 2012
    #11
  12. 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 <> 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)
     
    Ramchandra Apte, Sep 5, 2012
    #12
  13. loial

    Bryan Guest

    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
     
    Bryan, Sep 5, 2012
    #13
  14. On Wed, 5 Sep 2012 05:54:41 -0700 (PDT), Ramchandra Apte
    <> 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
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Sep 5, 2012
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jupiter5F

    Threads and Sockets

    Jupiter5F, Nov 14, 2003, in forum: C++
    Replies:
    4
    Views:
    2,326
    Jupiter5F
    Nov 14, 2003
  2. Julia Goolia

    tkinter, sockets and threads together

    Julia Goolia, Sep 11, 2003, in forum: Python
    Replies:
    4
    Views:
    547
    Steve Holden
    Sep 12, 2003
  3. Gonçalo Rodrigues

    Help needed in problem with Threads and sockets.

    Gonçalo Rodrigues, Dec 29, 2003, in forum: Python
    Replies:
    0
    Views:
    283
    Gonçalo Rodrigues
    Dec 29, 2003
  4. Rod Stephenson
    Replies:
    0
    Views:
    459
    Rod Stephenson
    Jun 22, 2004
  5. Martin Miller

    Signals and keyboard interupts

    Martin Miller, Oct 27, 2005, in forum: Python
    Replies:
    0
    Views:
    392
    Martin Miller
    Oct 27, 2005
Loading...

Share This Page