sockets,threads and interupts

Bryan bryanjugglercryptographer at yahoo.com
Wed Sep 5 12:02:39 EDT 2012


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



More information about the Python-list mailing list