#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25

    Sockets and Threading


    I am wanting to build a threading socket server. The probably is I can't seem to be able to exit no matter what. I have been using sockets for a while and I am fairly good, but I am building my first thread pooling server. I know everyone has problems with this jsut from my reasearch i have done into finding a good solution. I have deturmind that it is impossible to exit a thread that is running from outside the thread however I can exit from inside the thread by just ending it so I came up with this:

    Code:
    import Queue
    import socket
    try:
        import threading as _threading
    except ImportError:
        import dummy_threading as _threading
    
    def startNet():
    	#this is to start the hole networking interface
    	global clientPool
    	global stop
    	stop = False
    	clientPool = Queue.Queue ( 0 )
    	for x in xrange ( 40 ):
    		ClientThread().start()
    	server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
    	server.bind ( ( '', 2727 ) )
    	server.listen ( 5 )
    	while True:
    		if stop == True:
    			break
    		clientPool.put ( server.accept() )
    
    def setStopTrue():
    	#this sets a global verible to true to tell all running threads to exit when they get a chance
    	stop = True
    	
    class ClientThread ( _threading.Thread ):
    	def run ( self ):
    		while True:
    			if stop == True:
    				break
    			try:
    				client = clientPool.get(True, 1)
    			except Queue.Empty:
    				if stop == True:
    					break
    				client = None
    			if client != None:
    				print 'Received connection:%s', client [ 1 ] [ 0 ]
    				handle = handleConnection(client)
    				print 'Closed connection:', client [ 1 ] [ 0 ]
    
    class handleConnection:
    	def __init__(self, client):
    		self.client = client
    		self.sock = client[0]
    		self.work()
    	def work(self):
    		while 1:
    			data = self.sock.recv(4096)
    			if not len(data):
    				break
    			if stop == True:
    				self.sock.close()
    			self.sock.sendall(data)
    		self.sock.close()
    if __name__ == "__main__":
            try:
                     startNet()
            except KeyboardInterrupt:
    	         setStopTrue()
    	         print "hey"
    	         import sys
    	         sys.exit(0)
    well i think the concept is there however it doesn't seem to work, I know why it isn't working, its because the .lisen() method for the socket object will site there holding the main thread untill a connection is requested and then it will have a chance to check if set true is actually there, however then the query class with its .get method does the same thing till something is actually in it. So unless I get 40 connections and it gives a chance of all my pooled threads to end it wont stop even with the sys.exit(0) command because the app has to wait for the others to end.

    Any one got any ideas or sees something wrong with my implementation?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I don't get why you need to exit these threads. Why not just close the socket when necessary? Or simply "ignore" connectiosn from accept when the time is right.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    what if i want to exit the server?

    I want this to end up running from things such as windows services or rc.d in linux. I just want to amek sure it can end when it needs too, but with the way it is going it wont be able to. That is what I am looking for.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    There are a few options open to you:

    1) set a timeout on the socket - see the docs for the socket settimeout and setblocking methods.

    2) use the select module to poll the sockets at regular intervals

    3) use a higher level library such as asynccore

    Dave
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Well my orginal plan was to use asynccore, but I have read that it maybe slow. I am wanting to use this for a database type server, so speed is important, not important enough to use a faster language but I do want to get the most out of python as I can. So I figured I would go with threads so that each connection got full attention by the app. So what I am asking is, what is your experiance with ansyncore and how fast it runs?

IMN logo majestic logo threadwatch logo seochat tools logo