How to close a blocked socket?

Discussion in 'Python' started by Daniel T., Aug 28, 2003.

  1. Daniel T.

    Daniel T. Guest

    The code below fails. Socket.accept blocks inside the thread and doesn't
    let go, even after the socket was closed. From the error presented, the
    socket never actually closes.

    I realize that the below is a basic Java idiom but how do you do the
    same thing in Python?

    import unittest
    import socket
    import threading
    import time

    class SocketAcceptor ( threading.Thread ):
    def __init__( self, socket ):
    threading.Thread.__init__( self )
    self.socket = socket

    def run( self ):
    self.socket.bind( ( "", 3423 ) )
    self.socket.listen( 5 )
    child, ip = self.socket.accept()

    class SocketTester ( unittest.TestCase ):
    def testClose( self ):
    for each in range( 4 ):
    ss = socket.socket()
    acceptor_thread = SocketAcceptor( ss )
    acceptor_thread.start()
    time.sleep( 1 )
    ss.close()

    if __name__ == '__main__':
    unittest.main()
    Daniel T., Aug 28, 2003
    #1
    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. Ong Hong Peow
    Replies:
    3
    Views:
    466
    Ong Hong Peow
    Aug 25, 2004
  2. Yim

    How to awake blocked socket read?

    Yim, Feb 21, 2005, in forum: C Programming
    Replies:
    1
    Views:
    322
    Joona I Palaste
    Feb 21, 2005
  3. Iñaki Baz Castillo
    Replies:
    7
    Views:
    845
    Iñaki Baz Castillo
    Jan 12, 2010
  4. Alex Young
    Replies:
    7
    Views:
    120
    Robert Klemme
    Feb 13, 2011
  5. Mark
    Replies:
    9
    Views:
    129
Loading...

Share This Page