Python 的命令行程序时间久了打 log 就会弹异常

2018-01-24 10:21:12 +08:00
 gouchaoer

环境为 docker for win10,出异常的地方:

fn = self.logDir + "/%d-%s-%s.log" % (self.no, level, cate)
            file_object = open(fn, 'a')
            file_object.write(logLine)
            file_object.close()

异常为:

Traceback (most recent call last):
  File "/app/src/master.py", line 160, in <module>
  File "/app/src/master.py", line 155, in run
  File "/app/src/utils.py", line 133, in log
IOError: [Errno 5] Input/output error: '/app/var/log/0-warning-.log'

dockerfile 为:

FROM centos:6

RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm && \
yum -y install https://centos6.iuscommunity.org/ius-release.rpm  && \
yum -y install htop \ 
	iftop \
	git2u \
	vixie-cron \
	openssh-server \
	strace \
    python27 \
    python27-pip && \
yum clean all

RUN ( echo "root";sleep 1;echo "root" ) | passwd
RUN git config --global credential.helper store

ENV IMAGE_VER 0.3
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' > /etc/timezone


#不用 requirements.txt 而是分开安装在 docker 更快
#2.10.6
RUN pip2.7 install redis  -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com
#2.18.4
RUN pip2.7 install requests -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com
#4.6.0
RUN pip2.7 install BS4 -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com
#0.1.7
RUN pip2.7 install pytesseract -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com
#3.5.0
RUN pip2.7 install ConfigParser  -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com
#3.8.0
RUN pip2.7 install selenium  -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com

CMD service crond start && service sshd start && /bin/bash
2981 次点击
所在节点    Python
11 条回复
gouchaoer
2018-01-24 10:21:40 +08:00
实在搜不到解决方案,不知道啥原因导致的
gouchaoer
2018-01-24 10:25:00 +08:00
不止那个"/app/var/log/0-warning-.log"文件不能写,连别的文件也不能写:
```
Exception IOError: (5, 'Input/output error', '/app/var/log/0-info-.log') in <bound method Master.__del__ of <__main__.Master object at 0x7f6f7aa9b8d0>> ignored
```
我有点怀疑是因为我把 win 下的源码映射到 docker 容器的 /app 路径下,docker 容器是这么做的:
```
docker create -it --name test -v C:/Users/path/to/test:/app -p 23333:22 --cap-add SYS_PTRACE test:0.1
```
gouchaoer
2018-01-24 10:29:20 +08:00
可是事后在 docker 容器里用 vim 也可以打开编辑那些 log 文件啊,比较迷
gouchaoer
2018-01-24 10:30:18 +08:00
也没有别的程序以写或者读打开那些 log 文件
ipwx
2018-01-24 10:43:04 +08:00
看看文件句柄数量是不是超过了当前进程的限制数量。

https://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/

- - - -

每写一行打开一次文件不是好的 practice。请用 logging 模块。
MarcoQin
2018-01-24 10:45:46 +08:00
请用 logging 模块 +1
Shazoo
2018-01-24 10:48:51 +08:00
IOError: [Errno 5] Input/output error: '/app/var/log/0-warning-.log'

估计是 cate 参数为空导致的?感觉不是大问题。
yeyuexia
2018-01-24 10:49:10 +08:00
lz 是不是在程序里开了多线程 /多进程啊 总觉得有点像是写冲突的样子……所以还是老老实实用 logging 吧……
gouchaoer
2018-01-24 10:57:08 +08:00
这和 logging 模块无关啊,我只是觉得它太弱了我自己定制了一个,然后打开文件又写了点东西而已,那是个合法的文件名
@ipwx lsof -n | grep 5950 -c 查出句柄一直是 59,没有泄漏啥的
gouchaoer
2018-01-24 11:03:17 +08:00
总之先这样,多谢各位了:
```
#https://www.v2ex.com/t/425463
try:
fn = self.logDir + "/%d-%s-%s.log" % (self.no, level, cate)
file_object = open(fn, 'a')
file_object.write(logLine)
file_object.close()
fn = self.logDir + "/%d.log" % (self.no)
file_object = open(fn, 'a')
file_object.write(logLine)
file_object.close()
except Exception, e:
sys.stdout.write(e.message())
```
lolizeppelin
2018-01-24 20:13:44 +08:00
出错的时候 pwd 打出来

文件绝对路径打 出来 os path exit 上层目录看看

win 里切换过盘符没 切换了盘符没换回来出错正常

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/425463

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX