Python 的命令行程序时间久了打 log 就会弹异常 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
gouchaoer
V2EX    Python

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

  •  
  •   gouchaoer 2018 年 1 月 24 日 3531 次点击
    这是一个创建于 2909 天前的主题,其中的信息可能已经有所发展或是发生改变。

    环境为 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 
    11 条回复    2018-01-24 20:13:44 +08:00
    gouchaoer
        1
    gouchaoer  
    OP
       2018 年 1 月 24 日
    实在搜不到解决方案,不知道啥原因导致的
    gouchaoer
        2
    gouchaoer  
    OP
       2018 年 1 月 24 日
    不止那个"/app/var/log/0-warning-.log"文件不能写,连别的文件也不能写:
    ```
    Exception IOError: (5, 'Input/output erro', '/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
        3
    gouchaoer  
    OP
       2018 年 1 月 24 日
    可是事后在 docker 容器里用 vim 也可以打开编辑那些 log 文件啊,比较迷
    gouchaoer
        4
    gouchaoer  
    OP
       2018 年 1 月 24 日
    也没有别的程序以写或者读打开那些 log 文件
    ipwx
        5
    ipwx  
       2018 年 1 月 24 日
    看看文件句柄数量是不是超过了当前进程的限制数量。

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

    - - - -

    每写一行打开一次文件不是好的 practice。请用 logging 模块。
    MarcoQin
        6
    MarcoQin  
       2018 年 1 月 24 日
    请用 logging 模块 +1
    Shazoo
        7
    Shazoo  
       2018 年 1 月 24 日
    IOError: [Errno 5] Input/output error: '/app/var/log/0-warning-.log'

    估计是 cate 参数为空导致的?感觉不是大问题。
    yeyuexia
        8
    yeyuexia  
       2018 年 1 月 24 日
    lz 是不是在程序里开了多线程 /多进程啊 总觉得有点像是写冲突的样子……所以还是老老实实用 logging 吧……
    gouchaoer
        9
    gouchaoer  
    OP
       2018 年 1 月 24 日
    这和 logging 模块无关啊,我只是觉得它太弱了我自己定制了一个,然后打开文件又写了点东西而已,那是个合法的文件名
    @ipwx lsof -n | grep 5950 -c 查出句柄一直是 59,没有泄漏啥的
    gouchaoer
        10
    gouchaoer  
    OP
       2018 年 1 月 24 日
    总之先这样,多谢各位了:
    ```
    #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
        11
    lolizeppelin  
       2018 年 1 月 24 日 via Android
    出错的时候 pwd 打出来

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

    win 里切换过盘符没 切换了盘符没换回来出错正常
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2665 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 37ms UTC 03:19 PVG 11:19 LAX 19:19 JFK 22:19
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86