Reload doesn't work in debug mode when code contains the `threading.Timer()` function in it.
See original GitHub issueThe problem
My code was able to debug and was reloading when code was changed. After adding code which contained the threading.Timer()
function stops after code change in every file in project. It frustrated me when I had to start server again at every file change. It seems to be bug and if any one have a solution or experience, I will happy if helps me.
after file change it shows this result.
INFO: Application shutdown complete.
INFO: Finished server process [15791]
The code sample which causes this behavior is just like this.
def remove_expired_sessions(self):
threading.Timer(1200, self.remove_expired_sessions).start()
I found a way in order bypass this problem by adding a environment variable in os.environment
and check it in the code that causes this issue. However it prevent to debug the particular part of code which contains Timer
.
My temporary solution is like codes below.
In server file:
if mode == "development":
import os
os.environ['mode'] = 'development'
uvicorn.run("server:app", host=host, port=22659, debug=True, reload=True, headers=[("some", "other")])
in module:
def remove_expired_sessions(self):
if os.environ.get('mode') != 'development':
threading.Timer(1200, self.remove_expired_sessions).start()
Test platform and environment:
Python version: 3.8.5 vicorn version: 0.11.8 App: FastApi vesrion 0.61.1 OS: MacOs BigSur 11.1 Hardware: mac book pro 2018 core i9
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (3 by maintainers)
Top GitHub Comments
Hello,
I have the same issue on my side, Do you have any update, that will be very appreciate
thank 😃
closing for now, still lacks info to reproduce, feel free to reopen with it