Managing global objects with side effects when reloading a module in Python

224 Views Asked by At

I am looking for a way to correctly manage module level global variables that use some operating system resource (like a file or a thread).

The problem is that when the module is reloaded, my resource must be properly disposed (e.g. the file closed or the thread terminated) before creating the new one.

So I need a better pattern to manage those singleton objects.

2

There are 2 best solutions below

0
On BEST ANSWER

I've been reading the docs on module reload and this is quite interesting:

When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem. If the new version of a module does not define a name that was defined by the old version, the old definition remains. This feature can be used to the module’s advantage if it maintains a global table or cache of objects — with a try statement it can test for the table’s presence and skip its initialization if desired:

try:
    cache
except NameError:
    cache = {}

So I could just check if the objects already exist, and dispose them before creating the new ones.

0
On

You need to monkeypatch or fork django to hook into django dev server reloading feature and do the proper thing to manage file closing etc...

But since you develop a django application, if you mean to use a proper server to serve your app in the future you should consider managing your global variables and think about semaphores and all that jazz.

But before going this route and implement all this difficult code prone to error and hair loss. You should consider other solution like nosql databases (redis, mongodb, neo4j, hadoop...) and background process managers like celery and gearman. If all of this don't feet your use-case(s) and you can't avoid to create and manage files yourself and global variables then consider the client/server pattern where clients are webserver threads unless you want to mess with NFS.