螺纹.线程不工作,但线程工作



我有一些遗留的python 2.7代码,它们使用wxPython创建gui。gui代码使用_thread.start_new_thread启动从另一个模块导入的函数。我们正在将此代码转换为python 3.9,我想用threading.Thread替换_thread,这样我就可以使用thread.excepthook了。然而,当我用threading.Thread替换_thread时,我的代码就挂起了。

例如,我有:

import _thread
import custom_module
_thread.start_new_thread(custom_module.go, (arg1, arg2))

其中custom_module具有执行的函数go。这在Python 3.9中都很好,但我正在尝试使用threading.excepthook,所以我试图用以下内容替换它:

import threading
import custom_module
threading.Thread(target=custom_module.go, args=(arg1, arg2)).start()

但当我的代码到达线程时,它就挂起了。

_threadthreading之间是否存在会导致线程挂起threading而不是_thread的差异?

此外,在Python2.7中,我使用sys.excepthook来管理线程中发生的异常,但这已经不起作用了。当线程中发生异常时,线程就结束了。有什么想法吗?

=================================================================所以看起来问题不在于线程。线程不工作。我可以在custom_module.go函数的开头执行一条print语句。它实际上挂在主程序中证实的记录器的logger.info语句上。

def init_logger():
"""Initialize."""
logger = logging.getLogger('FDS')
logger.setLevel('INFO')
logging.Formatter.converter = time.gmtime
formatter = logging.Formatter('%(asctime)s.%(msecs)03d | %(levelname)-8s |' +
' %(name)s | %(message)s',
datefmt='%Y/%j-%H:%M:%S')
# set up log buffers (will flush into log file & GUI once started)
mh_file = logging.handlers.MemoryHandler(capacity=1000, flushLevel='DEBUG')
mh_file.setFormatter(formatter)
logger.addHandler(mh_file)
mh_gui = logging.handlers.MemoryHandler(capacity=1000, flushLevel='DEBUG')
mh_gui.setFormatter(formatter)
logger.addHandler(mh_gui)
logger.info('=' * 80)
logger.info('Starting FDS - Release ' + __version__ + '...')
# create application
logger.info('Building GUI...')
app = wx.App(False)
logger.debug('FDS GUI application created.', extra={'same_line':True})

我的代码挂在上

def go()
logger.info('Performing input validation...')
.
.
.

我想明白了。我用一个以前用来覆盖sys.exceptbook的函数来覆盖threading.exceptook,并且我没有更改该函数的参数(假设参数保持不变(。但是,在threading.excepthook重写函数中,只能提供一个参数(https://docs.python.org/3/library/threading.html)。当我修复我的线程能够正常工作并正确处理错误时。

最新更新