question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reload doesn't work in debug mode when code contains the `threading.Timer()` function in it.

See original GitHub issue

The 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kfdlcommented, May 13, 2022

Hello,

I have the same issue on my side, Do you have any update, that will be very appreciate

thank 😃

1reaction
euri10commented, May 27, 2021

closing for now, still lacks info to reproduce, feel free to reopen with it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does running the Flask dev server run itself twice?
The Werkzeug reloader spawns a child process so that it can restart that process each time your code changes. Werkzeug is the library...
Read more >
Learn to debug multithreaded applications - Visual Studio ...
To do this in the Breakpoint Settings window, select Filter instead of Conditional expression, and follow the filter tips. You may want to...
Read more >
Java: slow performance or hangups when starting debugger ...
Debugger performance can be affected by the following: Method breakpoints will slow down debugger a lot because of the JVM design, they...
Read more >
Python Multithreading Tutorial: daemon threads & join method
By setting them as daemon threads, we can let them run and forget about them, ... Note that if we do not have...
Read more >
Advanced Python Debugging with pdb | Codementor
pdb does not include a watch command by default, but you can get something similar by using commands , which lets you run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found