"OSError: [Errno 24] Too many open files:" with sockets from aiocoap context.request()

"OSError: [Errno 24] Too many open files:" with sockets from aiocoap context.request()

你好,我正在开发一个 raspberry PI,它应该从连接到这个 PI 的传感器连续发送数据,所以这是我的脚本:

import logging
import asyncio
from grovepi import*

import time
import json

from aiocoap import *

logging.basicConfig(level=logging.INFO)

def createMsg( file,value ) :
    f= open(file + "Config.json")
    data = json.load(f)
    f.close()
    data["timestamp"] = str(round(time.time(), 3))
    data["value"] = value
    return json.dumps( data )

async def main():

    #analog
    soundSensorPort=0
    lightSensorPort=1

    #digital
    motionSensorPort=2
    tempSensorPort=2
    ultrasonicSensorPort=5
    pinMode(tempSensorPort, "INPUT")

    while True:
        context = await Context.create_client_context()

        await asyncio.sleep(2)
        [ tempSensorValue, humiditySensorValue ]  = dht(tempSensorPort, 0)
        tempMsg = createMsg( "temperatureSensor", tempSensorValue)
        humidityMsg = createMsg( "humiditySensor", humiditySensorValue)

        payloadTemp = tempMsg.encode('utf-8')
        payloadHumidity = humidityMsg.encode('utf-8')
        requestTemp = Message(code=PUT, payload=payloadTemp)
        requestHumidity = Message(code=PUT, payload=payloadHumidity)

        requestTemp.opt.uri_host = '129.6.60.38'
        requestHumidity.opt.uri_host = '129.6.60.38'

        requestTemp.opt.uri_path = ("other", "sensors","temperature")
        requestHumidity.opt.uri_path = ("other", "sensors","humidity")

        responseTemp = await context.request(requestTemp).response
        responseHumidity = await context.request(requestHumidity).response

        print('Result: %s\n%r'%(responseTemp.code, responseTemp.payload))
        print('Result: %s\n%r'%(responseHumidity.code, responseHumidity.payload))

if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(main())

但是在 运行ning 大约 20/25 分钟后,我遇到了这个错误:

File "clientPUT.py", line 84, in <module>
  File "/usr/lib/python3.5/asyncio/base_events.py", line 466, in run_until_complete
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
  File "clientPUT.py", line 63, in main
  File "clientPUT.py", line 27, in createMsg
OSError: [Errno 24] Too many open files: 'temperatureSensorConfig.json'

这真的很奇怪,因为我在阅读文件并将其加载到 'f' 后就关闭了文件,所以如果有人有任何想法,那将非常有帮助

编辑 0: 运行 sudo ls -l /proc/4223/fd returns :

total 0
lrwx------ 1 root root 64 Jun 20 15:38 0 -> /dev/pts/0
lrwx------ 1 root root 64 Jun 20 15:38 1 -> /dev/pts/0
lrwx------ 1 root root 64 Jun 20 15:39 10 -> socket:[155862]
lrwx------ 1 root root 64 Jun 20 15:39 11 -> socket:[155876]
lrwx------ 1 root root 64 Jun 20 15:39 12 -> socket:[155877]
lrwx------ 1 root root 64 Jun 20 15:39 13 -> socket:[155878]
lrwx------ 1 root root 64 Jun 20 15:39 14 -> socket:[155879]
lrwx------ 1 root root 64 Jun 20 15:39 15 -> socket:[155880]
lrwx------ 1 root root 64 Jun 20 15:39 16 -> socket:[155882]
lrwx------ 1 root root 64 Jun 20 15:39 17 -> socket:[154981]
lrwx------ 1 root root 64 Jun 20 15:39 18 -> socket:[154982]
lrwx------ 1 root root 64 Jun 20 15:39 19 -> socket:[153455]
lrwx------ 1 root root 64 Jun 20 15:38 2 -> /dev/pts/0
lrwx------ 1 root root 64 Jun 20 15:39 20 -> socket:[156950]
lrwx------ 1 root root 64 Jun 20 15:39 21 -> socket:[156951]
lrwx------ 1 root root 64 Jun 20 15:39 22 -> socket:[156952]
lrwx------ 1 root root 64 Jun 20 15:39 23 -> socket:[156953]
lrwx------ 1 root root 64 Jun 20 15:39 24 -> socket:[153456]
lrwx------ 1 root root 64 Jun 20 15:39 25 -> socket:[156954]
lrwx------ 1 root root 64 Jun 20 15:39 26 -> socket:[156955]
lrwx------ 1 root root 64 Jun 20 15:39 27 -> socket:[156956]
lrwx------ 1 root root 64 Jun 20 15:39 28 -> socket:[156957]
lrwx------ 1 root root 64 Jun 20 15:39 29 -> socket:[156958]
lrwx------ 1 root root 64 Jun 20 15:38 3 -> /dev/i2c-1
lrwx------ 1 root root 64 Jun 20 15:39 30 -> socket:[156959]
lrwx------ 1 root root 64 Jun 20 15:39 31 -> socket:[156960]
lrwx------ 1 root root 64 Jun 20 15:39 32 -> socket:[156961]
lrwx------ 1 root root 64 Jun 20 15:39 33 -> socket:[156962]
lrwx------ 1 root root 64 Jun 20 15:39 34 -> socket:[156964]
lrwx------ 1 root root 64 Jun 20 15:39 35 -> socket:[156965]
lrwx------ 1 root root 64 Jun 20 15:39 36 -> socket:[156966]
lrwx------ 1 root root 64 Jun 20 15:39 37 -> socket:[156998]
lrwx------ 1 root root 64 Jun 20 15:39 38 -> socket:[154984]
lrwx------ 1 root root 64 Jun 20 15:39 39 -> socket:[155903]
lrwx------ 1 root root 64 Jun 20 15:38 4 -> anon_inode:[eventpoll]
lrwx------ 1 root root 64 Jun 20 15:39 40 -> socket:[155906]
lrwx------ 1 root root 64 Jun 20 15:39 41 -> socket:[153492]
lrwx------ 1 root root 64 Jun 20 15:39 42 -> socket:[153498]
lrwx------ 1 root root 64 Jun 20 15:39 43 -> socket:[153502]
lrwx------ 1 root root 64 Jun 20 15:39 44 -> socket:[155930]
lrwx------ 1 root root 64 Jun 20 15:39 5 -> socket:[155855]
lrwx------ 1 root root 64 Jun 20 15:39 6 -> socket:[155856]
lrwx------ 1 root root 64 Jun 20 15:38 7 -> socket:[155859]
lrwx------ 1 root root 64 Jun 20 15:39 8 -> socket:[155860]
lrwx------ 1 root root 64 Jun 20 15:39 9 -> socket:[155861]

鉴于 CoAP 既作为服务器又作为客户端工作的性质,即使当前没有请求处于活动状态,aiocoap 上下文也准备好对包做出反应,并一直持续到使用 .shutdown() 协程将其关闭.根据平台的不同,这通常意味着每个上下文至少创建一个服务器套接字,当创建太多时可能导致文件描述符耗尽。

除了极少数例外,应用程序没有理由创建多个上下文。由于这似乎是一个常见的误解,因此正在更新文档以使这一点更加明确。