“文件描述符超出 select() 范围”当使用带有 rsync 的 python 子进程时
下面的代码用于将上传的图片同步到另一个地方。它可以工作,但在一段时间(大约10天)后,该服务无法使用,显示错误:'filedescriptor out of range in select()',但重新启动服务解决了问题。
# sync.py
def sync_file(source_pic, hashval, retry_num=3):
pic_path = os.path.join(gen_dir(hashval), os.path.split(source_pic)[1])
filename = tempfile.mkstemp()[1]
with open(filename, 'w') as f:
f.write(pic_path)
for sync_path in options.sync_paths:
try_num = 0
rsync_cmd = ['rsync','-au', '--files-from='+filename, options.pic_path, sync_path]
while try_num < retry_num:
proc = subprocess.Popen(rsync_cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_value, stderr_value = proc.communicate()
if len(stderr_value) != 0:
logger.error('sync failed: %s' % stderr_value)
try_num = try_num + 1
#raise UploadException('sync failed')
else:
break
os.remove(filename)
日志信息:
File "/path/to/sync.py", line 25, in sync_file
stdout_value, stderr_value = proc.communicate()
File "/usr/lib/python2.6/subprocess.py", line 691, in communicate
return self._communicate(input)
File "/usr/lib/python2.6/subprocess.py", line 1211, in _communicate
rlist, wlist, xlist = select.select(read_set, write_set, [])
ValueError: filedescriptor out of range in select()
是否有未关闭的文件描述符导致错误?看来子进程没有关闭文件描述符,所以当它运行1024次时,文件描述符超出了范围。 (我们使用的是python 2.6,子进程被迫使用select.select(),即使epoll可用,它也有1024个文件描述符的限制)
the code below is used to sync uploaded picture to another place. it works, but after a period of time(about 10 days), the service is unusable , showing error: 'filedescriptor out of range in select()', but restart service solves the problem.
# sync.py
def sync_file(source_pic, hashval, retry_num=3):
pic_path = os.path.join(gen_dir(hashval), os.path.split(source_pic)[1])
filename = tempfile.mkstemp()[1]
with open(filename, 'w') as f:
f.write(pic_path)
for sync_path in options.sync_paths:
try_num = 0
rsync_cmd = ['rsync','-au', '--files-from='+filename, options.pic_path, sync_path]
while try_num < retry_num:
proc = subprocess.Popen(rsync_cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout_value, stderr_value = proc.communicate()
if len(stderr_value) != 0:
logger.error('sync failed: %s' % stderr_value)
try_num = try_num + 1
#raise UploadException('sync failed')
else:
break
os.remove(filename)
log info:
File "/path/to/sync.py", line 25, in sync_file
stdout_value, stderr_value = proc.communicate()
File "/usr/lib/python2.6/subprocess.py", line 691, in communicate
return self._communicate(input)
File "/usr/lib/python2.6/subprocess.py", line 1211, in _communicate
rlist, wlist, xlist = select.select(read_set, write_set, [])
ValueError: filedescriptor out of range in select()
are there unclosed file descriptors that cause the error? it seems subprocess doesn't close file descriptor, so when it runs 1024 times, the file descriptor is out of range. (we are using python 2.6, subprocess is forced to use select.select() which has a limit of 1024 file descriptors even epoll is available)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
https://bugzilla.redhat.com/show_bug.cgi?id=609020
可能的修复:使用 python 2.7+,或使用
poll
反向移植代码。https://bugzilla.redhat.com/show_bug.cgi?id=609020
possible fix: use python 2.7+, or backport the code using
poll
.您可以手动关闭文件描述符。调用
communicate
后,调用proc.stderr.close()
和proc.stdout.close()
。You can manually close the file descriptors. After calling
communicate
, callproc.stderr.close()
andproc.stdout.close()
.