线程无声崩溃并消失

时间:2017-01-30 10:28:13

标签: python multithreading raspberry-pi

我有一个线程在100毫秒内执行一次轮询操作。 一段时间后,当应用程序继续运行时,线程显然没有任何理由消失。

线程未被阻止,因为如果我打印应用程序中存在的所有线程,则轮询线程不存在。

代码如下:

def run(self):
    logger.info('PollThread thread started...')

    last_poll_time = curr_ms() / 1000
    while not self._stop_event.isSet():
        logger.debug('will do stuff')

        # doing stuff

        logger.debug('done!')

        # sleep until next polling cycle
        curr_time = curr_ms() / 1000
        logger.debug('thread present 1')
        time_from_last_poll = curr_time - last_poll_time

        if IO_POLLING_PERIOD > time_from_last_poll:
            sleep_period = IO_POLLING_PERIOD - time_from_last_poll
            if sleep_period > IO_POLLING_PERIOD:
                sleep_period = IO_POLLING_PERIOD
                logger.debug('thread present 2')

            logger.debug('thread present 2')
            time.sleep(sleep_period)
            logger.debug('thread present 3')

        logger.debug('thread present 4')
        last_poll_time = curr_ms() / 1000
        logger.debug('thread present 5')

    logger.info('terminated....')

curr_ms()以这种方式定义:

def curr_ms():
    return int(round(time.time() * 1000))

IO_POLLING_PERIOD是:

IO_POLLING_PERIOD = 0.1

打印“螺纹存在1”后,线程消失。 之后不会打印任何消息。

调试特别困难,因为问题可能会在运行几天后发生一次。 在Raspberry Pi 1上运行的代码。

已解决:由于日志字符串格式错误,线程崩溃了。在我上面提到的代码中,我删除了该行以阐明代码结构。添加try / catch后,我找到了问题。线程崩溃不会导致应用程序退出。

0 个答案:

没有答案
相关问题