码迷,mamicode.com
首页 > 其他好文 > 详细

select.select()文件句柄的限制

时间:2016-08-19 22:20:12      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:select 文件句柄

摘要

python中的select.select()函数调用了系统底层的select(),但是他有一个限制,就是当打开的文件句柄的数字达到1024后,就会出现下面的错误

ValueError: filedescriptor out of range in select()

正文

这个值在select()中对应的是FD_SETSIZE,下面我们写一段脚步来证明

#!/usr/bin/env python

import subprocess

import os

import select

import getpass

host = ‘example.com‘

timeout = 5

password = getpass.getpass(prompt="Enter your password:")

for i in range(1000):

        (rfd, wfd) = os.pipe()

        ssh_cmd = [‘sshpass‘, ‘-d%d ‘ % rfd]

        ssh_cmd += [‘ssh‘, ‘%s‘ % host, ‘uptime‘]

        print ssh_cmd

        os.write(wfd, password + ‘\n‘)

        p = subprocess.Popen(ssh_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        rfd, wfd, efd = select.select([p.stdout, p.stderr], [], [p.stderr], timeout)

        if rfd:

                print p.stdout.read()

                print p.stderr.read()


相关的文章可以参考

https://github.com/ansible/ansible/issues/10157

https://issues.apache.org/jira/browse/QPID-5588

https://github.com/ansible/ansible/issues/14143


本文出自 “Linux运维” 博客,谢绝转载!

select.select()文件句柄的限制

标签:select 文件句柄

原文地址:http://haohaozhang.blog.51cto.com/9176600/1840436

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!