FAQ
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()

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 1 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedAug 28, '03 at 2:04a
activeAug 28, '03 at 2:04a
posts1
users1
websitepython.org

1 user in discussion

Daniel T.: 1 post

People

Translate

site design / logo © 2022 Grokbase