使用线程模块完成测试后 Django 不会退出
Django does not exit after finishing tests when using threading module
我正在使用 django.test.SimpleTestCase
进行集成测试。
在 运行ning python manage.py test
之后,测试 运行 成功并且终端挂起并显示消息:
---------------------------
Ran 5 tests in 1.365s
OK
问题是目前我使用 CTRL+C 返回终端,但我想在我的 CI/CD 管道中进行自动化测试。
我执行测试的方式有问题吗?或者这种行为正常吗?在这种情况下,Bash 中有没有办法以编程方式执行然后退出测试?
编辑:
在深入分析我的应用程序后,我能够确定导致该行为的原因。我在 views.py
:
中以如下方式使用 threading
def __pooling():
wait_time = 10
call_remote_server()
threading.Timer(wait_time, __pooling).start()
__pooling()
基本上我需要我的应用程序不时地异步执行某些操作。
我应该改变我进行汇集的方式吗?或者我应该在测试期间禁用它(如何?)?
I was able to identify what was causing that behaviour. I am using threading
in a way like the following in my views.py
:
def __pooling():
wait_time = 10
call_remote_server()
threading.Timer(wait_time, __pooling).start()
__pooling()
Basically I need that my application do something from time to time
asynchronously. Should I change the way I am doing the pooling?
我不完全了解您的需求,但更传统的方法是安排任务(可能 management command) outside of Django itself. An OS-level scheduler like cron or Windows Task Scheduler, something like APScheduler, or a task queue like Celery 都是合理的选择。
Or should I disable it (how?) during the tests?
我不建议继续使用您现在的 __pooling()
功能。在我看来,这种东西不属于你views.py
。但是如果你想保留它,比如
from django.conf import settings
if not settings.DEBUG:
__pooling()
可能会有帮助。您的 __pooling()
函数只会在 DEBUG
为假时调用,因为它应该在生产中。 (如果它在您的 CI 环境中也是错误的,您可以选择另一个现有设置,或者在您的 settings.py
中添加一些东西来专门控制它。)
我正在使用 django.test.SimpleTestCase
进行集成测试。
在 运行ning python manage.py test
之后,测试 运行 成功并且终端挂起并显示消息:
---------------------------
Ran 5 tests in 1.365s
OK
问题是目前我使用 CTRL+C 返回终端,但我想在我的 CI/CD 管道中进行自动化测试。
我执行测试的方式有问题吗?或者这种行为正常吗?在这种情况下,Bash 中有没有办法以编程方式执行然后退出测试?
编辑:
在深入分析我的应用程序后,我能够确定导致该行为的原因。我在 views.py
:
threading
def __pooling():
wait_time = 10
call_remote_server()
threading.Timer(wait_time, __pooling).start()
__pooling()
基本上我需要我的应用程序不时地异步执行某些操作。 我应该改变我进行汇集的方式吗?或者我应该在测试期间禁用它(如何?)?
I was able to identify what was causing that behaviour. I am using
threading
in a way like the following in myviews.py
:def __pooling(): wait_time = 10 call_remote_server() threading.Timer(wait_time, __pooling).start() __pooling()
Basically I need that my application do something from time to time asynchronously. Should I change the way I am doing the pooling?
我不完全了解您的需求,但更传统的方法是安排任务(可能 management command) outside of Django itself. An OS-level scheduler like cron or Windows Task Scheduler, something like APScheduler, or a task queue like Celery 都是合理的选择。
Or should I disable it (how?) during the tests?
我不建议继续使用您现在的 __pooling()
功能。在我看来,这种东西不属于你views.py
。但是如果你想保留它,比如
from django.conf import settings
if not settings.DEBUG:
__pooling()
可能会有帮助。您的 __pooling()
函数只会在 DEBUG
为假时调用,因为它应该在生产中。 (如果它在您的 CI 环境中也是错误的,您可以选择另一个现有设置,或者在您的 settings.py
中添加一些东西来专门控制它。)