繁体   English   中英

从线程登录:登录到INFO或更高版本时退出线程,登录到DEBUG时正常运行

[英]Logging from thread: Thread exits when logging to INFO and higher, runs normally when logging to DEBUG

我正在编写一个提供PySide UI的程序。 在其中启动一个线程,该线程设置应该在后台运行的一系列功能,而UI则显示进度条。

我使用Python 3的的反向移植concurrent.futures为Python 2.7多线程。

UI方法如下所示:

def doPostprocess(self):
    with ThreadPoolExecutor(max_workers=1) as executor:
        future = executor.submit(othermodule.func)
        while not future.done():
            QtGui.qApp.processEvents()
            self.progressbar.setValue(1)
            time.sleep(0.001)
    self.progressbar.hide()

这是我最小的othermodule.func样子:

def func():
    logger.info("Some informational message")
    time.sleep(15)
    print "And we are done here"

永远不会将“我们在这里完成”打印到stdout,但是future对象会发出信号,要求在调用logger.info之后立即完成。

有趣的是:当我将调用更改为logger.debug ,一切都按预期工作,即func日志,休眠15秒,然后打印到stdout,同时主线程更新其进度栏。 无论为应用程序设置什么日志级别,都会发生这种情况。

记录器对象如何配置? 可以为不同的日志级别配置不同的处理程序,并且它们可能会失败。 参见https://docs.djangoproject.com/zh-CN/1.3/topics/logging/#configuring-logging

另请参阅此说明http://docs.python.org/2.7/library/logging.html?highlight=logging#thread-safety 可能是这样。

更新

您也可以尝试使用包罗万象的异常处理程序来查看线程内部发生了什么。 像这样的东西:

def func():
    try:
        logger.info("Some informational message")
        time.sleep(15)
        print "And we are done here"
    except:
        print "We are interrupted"
        pprint.pprint(sys.exc_info())

UPDATE2:

http://hg.python.org/cpython/file/af18829a7754/Lib/concurrent/futures/_base.py#l343中所示 done()方法仅返回worker的状态,并且不会引发异常。

您可以检查将来的exception()方法是否存在异常。 同样,这将从func()删除不必要的异常处理。

您的代码可能是(从worker中引发异常):

def doPostprocess(self):
with ThreadPoolExecutor(max_workers=1) as executor:
    future = executor.submit(othermodule.func)
    while not future.done():
        QtGui.qApp.processEvents()
        self.progressbar.setValue(1)
        time.sleep(0.001)
    if future.exception() is not None:
        raise future.exception()
self.progressbar.hide()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM