Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当flask服务器运行另一个线程时,Pytest挂起_Python_Unit Testing_Flask_Pytest_Flask Testing - Fatal编程技术网

Python 当flask服务器运行另一个线程时,Pytest挂起

Python 当flask服务器运行另一个线程时,Pytest挂起,python,unit-testing,flask,pytest,flask-testing,Python,Unit Testing,Flask,Pytest,Flask Testing,我使用的是Python3、烧瓶0.12和Pytest 3.0.7 我有一个类似的烧瓶应用程序: class AppInitializer: def __init__(self): pass @staticmethod def __function_to_be_refreshed(): while True: try: time.sleep(5) except

我使用的是Python3、烧瓶0.12和Pytest 3.0.7

我有一个类似的烧瓶应用程序:

class AppInitializer:
    def __init__(self):
        pass

    @staticmethod
    def __function_to_be_refreshed():
        while True:
            try:
                time.sleep(5)
            except Exception as e:
                logger.exception(e)


    def create_app(self):
        daemon_thread = threading.Thread(target=self.__function_to_be_refreshed)
        daemon_thread.daemon = True
        daemon_thread.start()
        atexit.register(daemon_thread.join)
        app_ = Flask(__name__)
        return app_


app_initializer = AppInitializer()
app = app_initializer.create_app()
我正在尝试使用pytest测试此应用程序,如下所示:

import unittest

import pytest


class TestCheckPriceRequestAPI(unittest.TestCase):
    def setUp(self):
        self.app = api.app.test_client()

    def test_random(self):
        pass

当我使用
pytest
运行此测试时,此测试(以及所有其他测试)成功运行,但pytest挂起。如何停止正在运行的pytest进程(或者可能杀死守护进程线程)

join命令只意味着线程将等待线程完成,但不会完成它。要使用无止境循环结束线程,可以执行以下操作:

class AppInitializer:
    def __init__(self):
        self._run = True
        self._daemon_thread = None

    def __function_to_be_refreshed(self):
        while self._run:
            try:
                time.sleep(5)
            except Exception as e:
                logger.exception(e)

    def __shutdown(self):
        self._run = False
        if self._daemon_thread:
            self._daemon_thread.join()

    def create_app(self):
        self._daemon_thread = threading.Thread(target=self.__function_to_be_refreshed)
        ...
        atexit.register(self.__shutdown)
        ...

嗯,烧瓶应用程序没有业务产生threads@e4c5:这是项目中增加的一项要求。我对完成线程和终止线程感到困惑。谢谢你的建议:)