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-01-24 10:21:12 +08:00 · 2974 次点击
    这是一个创建于 2276 天前的主题,其中的信息可能已经有所发展或是发生改变。

    环境为 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-01-24 10:21:40 +08:00
    实在搜不到解决方案,不知道啥原因导致的
    gouchaoer
        2
    gouchaoer  
    OP
       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
        3
    gouchaoer  
    OP
       2018-01-24 10:29:20 +08:00
    可是事后在 docker 容器里用 vim 也可以打开编辑那些 log 文件啊,比较迷
    gouchaoer
        4
    gouchaoer  
    OP
       2018-01-24 10:30:18 +08:00
    也没有别的程序以写或者读打开那些 log 文件
    ipwx
        5
    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
        6
    MarcoQin  
       2018-01-24 10:45:46 +08:00
    请用 logging 模块 +1
    Shazoo
        7
    Shazoo  
       2018-01-24 10:48:51 +08:00
    IOError: [Errno 5] Input/output error: '/app/var/log/0-warning-.log'

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

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

    win 里切换过盘符没 切换了盘符没换回来出错正常
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5265 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:50 · PVG 15:50 · LAX 00:50 · JFK 03:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.