safely closing a socket?

Russell E. Owen rowen at
Fri Sep 26 22:50:30 CEST 2003

I've been doing some socket programming with threads and am confused how 
to safely shut down a connection.

First of all, using the file-like object returned by urllib to do ftp 
file download (Python 2.3, MacOS X), I can hang the process by:
- using one thread to read the file-like object
- trying to close the connection in the main thread (while still 
it's not clear to me what's hung, but it just sits there. No major CPU 
cycles being eaten. Some kind of deadlock, perhaps???

I've worked around the problem by setting a flag and having the read 
thread quit. But this can take quite awhile. Is there some fast safe way 
to terminate such a download?

Also, is this more generally an issue with sockets? Is it dangerous to 
close a socket while another thread may be reading or writing it?

Any hints or pointers to information would be much appreciated.

-- Russell

More information about the Python-list mailing list