title: Python Socket gives "[Errno 24] Too many open files"
body: <p>I have the following UDP class sending arrays of data at about 100Hz</p>
  
  <pre><code>from six import string_types
  import socket
  import struct
  
  def convert_data(iterable):
      if isinstance(iterable, string_types):
          return str(iterable)
      data = tuple(iterable)
      format = "{0}H".format(len(data))
      print("Sending data:", format, data)
      if max(data) &gt; 2**16 - 1:
          raise ValueError(max(data))
      if min(data) &lt; 0:
          raise ValueError(min(data))
      return struct.pack(format, *data)
  
  class UDP(object):
      def __init__(self, ip, port):
          self._ip = ip
          self._port = port
          self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
          self.socket.connect((ip, port))
  
      def send_data(self, data):
          message = convert_data(data)
          return self.socket.sendall(message)
  </code></pre>
  
  <p>It gives the following error after <strong>successfully sending</strong> for about a minute:</p>
  
  <pre><code>Traceback (most recent call last):
    File "take_analogue_data.py", line 13, in &lt;module&gt;
    File "take_analogue_data.py", line 8, in main
    File "/home/pi/nio-integration/hardware/raspi/UDP.py", line 22, in __init__
    File "/usr/lib/python2.7/socket.py", line 187, in __init__
  socket.error: [Errno 24] Too many open files
  </code></pre>
  
  <p>I have looked for a solution. <a href="https://stackoverflow.com/questions/2569620/socket-accept-error-24-to-many-open-files">This Stack Overflow answer suggests increasing the number of possible files</a>. I really don't think this is the solution I am looking for though.</p>
  
  <p>Is there something I can do? I was thinking that closing the connection each time might work, but I have already played around with a bunch of things. (I have tried <code>send</code>, <code>sendall</code>, and <code>sendto</code> -- none have worked)</p>
  
  <p><strong>Note: I am running Python2.6 on Raspbian Wheezy on a Raspberry Pi</strong></p>
  
  <p><strong>Edit</strong>
  Another module is sending the data. It could look something like</p>
  
  <pre><code>import UDP
  udp = UDP.UDP(IP, PORT)
  while(True):
      udp.send_data(range(8))
      sleep(0.01)
  </code></pre>