Python Socket gives "[Errno 24] Too many open files"
I have the following UDP class sending data arrays at around 100Hz
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) > 2**16 - 1:
raise ValueError(max(data))
if min(data) < 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)
It gives the following error after successful submission within a minute:
Traceback (most recent call last):
File "take_analogue_data.py", line 13, in <module>
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
I was looking for a solution. This answer suggests increasing the number of possible files . I don't really think this is the solution I'm looking for though.
Is there something I can do? I thought that closing the connection every time might work, but I've already played with a bunch of things. (I tried it send
, sendall
and sendto
- no one did not work)
Note. I am running Python2.6 on Raspbian Wheezy on a Raspberry Pi
Edit Another module is sending data. It might look something like
import UDP
udp = UDP.UDP(IP, PORT)
while(True):
udp.send_data(range(8))
sleep(0.01)
source to share
You are probably creating a new socket for each separate iteration while(True):
. Processes are limited by the number of file descriptors they can open (sockets are fds.) You can check /etc/security/limits.conf
to see what limits are set.
You should close your socket when you're done with it, or ideally just open it and use it if possible.
You said that your other module "might look something like this". Is this piece of code exactly what it looks like?
I doubt it because if it is, then only one socket needs to be done. If you are creating a UDP object in while
then this is definitely your problem.
source to share
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))
What you should know about UDP is that it is a session less and connectionless protocol, which means that: self.socket.connect((ip,int(port)))
is incorrect, so you must remove it.
If you want to connect to the server, use Tcp instead:
class TCP(object):
def __init__(self, ip, port):
self._ip = ip
self._port = port
self.socket = socket.socket() #Here is the change
self.socket.connect((ip, port))
def send_data(self, data):
message = convert_data(data)
return self.socket.sendall(message)
Hope this helped!
source to share