python守护线程,在Python中打印守护线程异常

Python does not print traceback messages from exceptions raised in daemon threads.

For example, this code creates a daemonic thread and raises an exception in the new thread:

def error_raiser():

raise Exception

import threading

thread = threading.Thread(target=error_raiser)

thread.daemon = True

thread.start()

but does not print a traceback. (It gives no output).

However, if the thread is not set as a daemon thread, Python will print the traceback. Here is the same code with one line commented out:

def error_raiser():

raise Exception

import threading

thread = threading.Thread(target=error_raiser)

# thread.daemon = True

thread.start()

and the output:

Exception in Thread-1:

Traceback (most recent call last):

File "C:\Python26\lib\threading.py", line 525, in __bootstrap_inner

self.run()

File "C:\Python26\lib\threading.py", line 477, in run

self.__target(*self.__args, **self.__kwargs)

File "test.py", line 2, in error_raiser

raise Exception

Exception

Executing this code in both Python 2.6.2 and Python 3.0.1 and gives the same results. Interestingly, however, if I execute the code by importing it in the IPython shell, the exception is displayed whether the thread is daemonic or not.

According to the documentation, the only significance of the 'daemon' flag is that "the entire Python program exits when only daemon threads are left." This would make me believe that not printing a traceback after an exception is a bug in Python, unless I missed something in the documentation.

Is this a bug, or did I miss something in the documentation and this behavior is intentional? If it is intentional, how can I force Python to print the traceback in daemon threads without using IPython?

解决方案

According to Wikipedia, by definition a daemon should detach itself from the controlling tty, so I think it's correct that no exception is shown (and after all, a daemon should keep working even if you close the shell that launched it)..

See here.

As for how to print the traceback, I think that a simple try/except_then_log_to_file would do the trick :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么学爬虫?】        1、爬虫入手容易,但是深入较难,如何写出高效率的爬虫,如何写出灵活性高可扩展的爬虫都是一项技术活。另外在爬虫过程,经常容易遇到被反爬虫,比如字体反爬、IP识别、验证码等,如何层层攻克难点拿到想要的数据,这门课程,你都能学到!        2、如果是作为一个其他行业的开发者,比如app开发,web开发,学习爬虫能让你加强对技术的认知,能够开发出更加安全的软件和网站 【课程设计】 一个完整的爬虫程序,无论大小,总体来说可以分成三个步骤,分别是:网络请求:模拟浏览器的行为从网上抓取数据。数据解析:将请求下来的数据进行过滤,提取我们想要的数据。数据存储:将提取到的数据存储到硬盘或者内存。比如用mysql数据库或者redis等。那么本课程也是按照这几个步骤循序渐进的进行讲解,带领学生完整的掌握每个步骤的技术。另外,因为爬虫的多样性,在爬取的过程可能会发生被反爬、效率低下等。因此我们又增加了两个章节用来提高爬虫程序的灵活性,分别是:爬虫进阶:包括IP代理,多线程爬虫,图形验证码识别、JS加密解密、动态网页爬虫、字体反爬识别等。Scrapy和分布式爬虫:Scrapy框架、Scrapy-redis组件、分布式爬虫等。通过爬虫进阶的知识点我们能应付大量的反爬网站,而Scrapy框架作为一个专业的爬虫框架,使用他可以快速提高我们编写爬虫程序的效率和速度。另外如果一台机器不能满足你的需求,我们可以用分布式爬虫让多台机器帮助你快速爬取数据。 从基础爬虫到商业化应用爬虫,本套课程满足您的所有需求!【课程服务】 专属付费社群+定期答疑
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值