摘要
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