推荐学习书目
› 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
shiweifu
V2EX  ›  Python

Python 多线程跑着跑着挂了,该如何处理

  •  
  •   shiweifu ·
    shiweifu · May 27, 2012 · 5507 views
    This topic created in 5286 days ago, the information mentioned may be changed or developed.
    在写一个爬虫,用Queue配合thread来实现线程池,爬完一个地址,调用一次queue.task_done()
    但在爬的过程中,有可能出一些Error,比如TimeOutError什么的,这样的话线程就挂了,永远也不会返回,也就调用不了queue.task_done(),最后就卡住了

    请问这种情况该如何处理?

    我想的是重写个queue.join(),在其中判断queue是否为空,如果是空的,就说明已经都爬完了,然后就退出,有没有更好的解决方法?
    11 replies  •  1970-01-01 08:00:00 +08:00
    quake0day
    quake0day
    May 27, 2012❤️2
    1
    try catch pass 不行?
    Livid
    Livid
    MOD
    PRO
    May 27, 2012❤️2
    2
    用 try catch 处理异常。
    shiweifu
    shiweifu
    OP
    May 27, 2012
    3
    @quake0day Exception能捕获,Error捕获不了,直接就挂了 = =!
    INT21H
    INT21H
    May 27, 2012❤️1
    4
    global队列,然后 while len(q) > 0:
    try-except判断是否timeout,若timeout,该线程q.get()

    不能让一个线程挂掉
    Livid
    Livid
    MOD
    PRO
    May 27, 2012❤️2
    5
    或者你可以重新写一下你的程序,用 celery 来实现多任务并行处理。
    sivacohan
    sivacohan
    May 27, 2012❤️1
    6
    守护进程。。。。
    我是这么干的,一会检查一下那些进程挂掉了。。。。
    clowwindy
    clowwindy
    May 27, 2012❤️1
    7
    try except finally 为什么不行?楼主贴一下 except 代码,和异常堆栈信息看看。
    reus
    reus
    May 27, 2012❤️1
    8
    用multiprocessing.Pool
    shiweifu
    shiweifu
    OP
    May 28, 2012
    9
    @all 已解决,谢谢大家
    gonbo
    gonbo
    May 28, 2012
    10
    scrapy 这种框架已经很无敌啦。。。。
    zealot
    zealot
    Jun 9, 2012
    11
    scrapy +1
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1118 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 18:13 · PVG 02:13 · LAX 10:13 · JFK 13:13
    ♥ Do have faith in what you're doing.