__init__ OSError中的文件“ /usr/lib/python3.5/socket.py”,第134行,[Errno 24]打开的文件太多

时间:2018-07-16 15:18:16

标签: python python-3.x sockets raspberry-pi3 serversocket

我在树莓派Pi3的while循环中调用广播和接收数据功能。在while循环之后,我想调用另一个函数,该函数会将帧发送到他的邻居节点,但是当该函数(send_req_status_frame())调用时,它会给出错误

Traceback (most recent call last):
  File "Neighbortest.py", line 347, in <module>
  File "Neighbortest.py", line 286, in send_req_status_frame
  File "/usr/lib/python3.5/socket.py", line 134, in __init__
OSError: [Errno 24] Too many open files

它在“ send_req-status-frame”功能的“ s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)”行上给出错误。 谁能帮我解决这个错误?

def send_req_status_frame():           
    message='req_status'
    previous_ip=My_ip
    sender_ip=My_ip 
    T_message= message + ";" + previous_ip + ";" + sender_ip 
    T_message_bytes= bytes(T_message,'utf-8')
    print ("----Send_req_status_frame: Send to    My_Neighborlist",MyNeighborSet_ip)
    PORT = 12345

    print ("just after socket")
    for i in range (len(MyNeighborSet_ip)):
        s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        s.connect((MyNeighborSet_ip[i],PORT))
        s.sendall (T_message_bytes)   #here is issue of 'byte' 
        s.close()   

start_time = time.time() temp = parallel.futures.ThreadPoolExecutor(max_workers = 2)

while(((time.time()-start_time)<NeighborDiscovery_Time_Interval) and (len(MyNeighborSet_ip) <= Nmax_Per_hop)):
    if (time.time()-start_time<hello_Broadcast_Period):
        broadcast_hello()        
    try:
        temp.submit(received_hello)
    except:
        print("###################")

send_req_status_frame()

1 个答案:

答案 0 :(得分:0)

如果使用的是Mac OS或Linux版本,则可以查看打开文件的限制,然后增加该值。

# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1064
virtual memory          (kbytes, -v) unlimited

要增加限制(在RPI3上为65536,在Mac OSX上为18432):

# ulimit -Sn 5000