使用python的selectors模块时, 出现描述符过多的ValueError异常, 如何修改最大文件描述符上限?

  • 环境

    python version >= (3,4,0)
    win10系统

  • 异常位置

      if sys.platform == 'win32':
          def _select(self, r, w, _, timeout=None):
              # 由于描述符过多,在下面抛出了ValueError异常
              r, w, x = select.select(r, w, w, timeout)
              return r, w + x, []

  • 异常堆栈

    Traceback (most recent call last):
    File "multiio_client.py", line 70, in <module>
      result = client.run()
    File "multiio_client.py", line 50, in run
      events = self.select.select()
    File "E:\pythoninstall\lib\selectors.py", line 324, in select
      r, w, _ = self._select(self._readers, self._writers, [], timeout)
    File "E:\pythoninstall\lib\selectors.py", line 315, in _select
      r, w, x = select.select(r, w, w, timeout)
    ValueError: too many file descriptors in select()

  • 猜测

    windows和linux的进程最大文件描述符分别是509, 1024,需要改变内核中的宏定义,或者通过一些类似的系统调用来改变

/* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
   64 is too small (too many people have bumped into that limit).
   Here we boost it.
   Users who want even more than the boosted limit should #define
   FD_SETSIZE higher before this; e.g., via compiler /D switch.
*/
#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
#define FD_SETSIZE 512
#endif

我没有问题了。。。发现自己都弄明白了
阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进