下面的代碼用於將上傳的圖片同步到另一個地方。它工作,但經過一段時間(大約10天)後,該服務無法使用,顯示錯誤:'filedescriptor在select()'中超出範圍,但重新啓動服務可解決問題。「當使用python的rsync子進程時,select()中的filedescriptor超出範圍」
# 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(),它有1024個文件描述符的限制,甚至epoll可用)
終於我刪除PIPE重定向時,Popen,並檢查'proc.returnval',它似乎工作。 – limboy