Search code examples
pythonsockets

Python: better way to open lots of sockets


I have the following program to open lot's of sockets, and hold them open to stress test one of our servers. There are several problem's with this. I think it could be a lot more efficient than a recursive call, and it's really still opening sockets in a serial fashion rather than parallel fashion. I realize there are tools like ab that could probably simulate what I'm trying to do, but I'm hoping to increase my python knowledge. Is this something I should be rewriting as either multi-threaded or multi-process?

> #!/usr/bin/env python
> 
> import socket, time, sys
> sys.setrecursionlimit(2000)
> 
> def open_socket(counter):   
>   sockname = "s" + str(counter)   
>   port = counter + 3000   
>   sockname = socket.socket()  
>   sockname.bind(('localhost', port))  
>   sockname.listen(1)   
>   if counter == 2:
>     time.sleep(20)   
>   elif counter > 2:
>     counter = counter -1
>     open_socket(counter)
> 
> open_socket(1500)

Solution

  • I was puzzled why you would use recursion instead of a simple loop. My guess is that with a simple loop, you would have overwritten the variable sockname again and again, so that Python's garbage collection would actually close the previous socket after you created the next one. The solution is to store them all in a list, to prevent Python from garbage-collecting them:

    def open_socket(counter):
      sockets = []
      for i in range(counter):
         s = socket.socket()
         s.bind(('localhost', i+3000))
         s.listen(1)
         sockets.append(s)
      time.sleep(20)
    

    Also notice that in your code, the first assignment to sockname is completely redundant, as it is overwritten by the second assignment.