How to terminate a Uvicorn + FastAPI application cleanly with workers gt;= 2 when testing with pytest(如何在使用pytest测试时使用gt;=2干净利落地终止Uvicorn+FastAPI应用程序)
问题描述
我有一个用Uvicorn+FastAPI编写的应用程序。 我正在使用PyTest测试响应时间。
参考How to start a Uvicorn + FastAPI in background when testing with PyTest,我编写了测试。 但是,当工人>;=2时,我在完成测试后发现申请过程仍然有效。
我希望在测试结束时干净地终止应用程序进程。
您有什么想法吗?
详细信息如下。
环境
- Windows 10
- Bash 4.4.23(https://cmder.net/)
- python 3.7.5
库
- fast api==0.68.0
- uvicorn==0.14.0
- 请求==2.26.0
- pytest==6.2.4
示例代码
- 应用:main.py
- 测试:test_main.py
执行结果
推荐答案
我自己找到了解决方案。
谢谢>;https://stackoverflow.com/a/27034438/16567832
解决方案
pip install psutil
安装psutil后,更新test_main.py
执行结果
这篇关于如何在使用pytest测试时使用>;=2干净利落地终止Uvicorn+FastAPI应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!