When I am debugging and reload some Python Code, I would like to keep some state in memory

My code keeps some queues in singletons memory. Whenever I edit and  reload any Python code, this state is zeroed out. Is there a way to keep singletons  in memory through a reload event? The code being edited is not  the module that holds the queues.

I have tried keeping state in globals, in a dynamically defined module, etc. I would prefer to keep the state in memory, not in files.

The project pydevd_reload says that it doesn't "break things such as singletons." I'd like to achieve that in the default PyCharm setup. Indeed, if I can write my singletons to work this way in any hot-reload system (so that this will work in other IDEs) that would be best.

Comment actions Permalink

This seems more like a general Python question, rather than IDE-specific question.

Perhaps you can use wrapper script with reload function?


Comment actions Permalink

Thank you. The question applies to any IDE where code is hot-reloaded, though of course the solution will be based on  general Python mechanisms. I am not sure if PyCharm's reloading is completely standard, and if not, there may be special considerations.

I can't use a wrapper script because this singleton is in a reusable library, so that the state that must stay un-wiped lives in the core, not in an external script that calls the core. (And also, I can't require special structure from users for this library.)

Comment actions Permalink

>The question applies to any IDE where code is hot-reloaded

By "reload", do you mean re-running your run/debug configuration? As far as I know, PyCharm doesn't have any hot reloading features. When you launch a run/debug configuration, PyCharm calls the python interpreter to run the code, same way as if you would call it from the command line. The IDE doesn't provide any functionality to keep data in memory between re-runs.

If anything is to be done to solve it, it should be done from the code.

Comment actions Permalink

> As far as I know, PyCharm doesn't have any hot reloading features

If you are in the debugger and then edit and save some code, then PyCharm will immediately reload all modules.

Comment actions Permalink

I'm not sure PyCharm does this. When you edit the source code, you have to re-run the debugger to update on the changes.

Are you using any custom plugins that does this? Maybe you could provide an example of this hot-reload behavior, just so that we're on the same page?

Comment actions Permalink

Andrey, thank you for the hint. The reloading in fact comes from Flask, as even in this minimal reproduction, where changing value. then saving, causes a reload. Of course, real code would have multiple modules, which could provide the way to exclude the reloading.

from flask import Flask
value = 'The value'
app = Flask(__name__)
def root():
return value

if __name__ == "__main__":
app.run(host="", port=8990, debug=True)
Comment actions Permalink

Ok, understood, thanks for clarification. The reloading functionality in this case is handled by the Flask server, not PyCharm.

As I mentioned, PyCharm's debugger itself doesn't have a reloading functionality like that, so I'm unable to advice any workflow to solve your problem. Sorry for not being able to help.

Comment actions Permalink

You can try my PyCharm plugin Reloadium that adds hot reloading ( aka edit and continue) to any python application.



If you have objects in your code that you don't wan't to reload you might need to wrap them with # reloadium: no_reload as described here:



Please sign in to leave a comment.