On-demand singleton for Python

Some singletons eat slightly more resources, than you want to give them for free. For instance, if you have a home-brew threadpool singleton, you don’t want it to create its threads if you are not going to use it. The solution: a simple function that creates a stub which proxies attribute access to an ad-hoc created instance.

Usage: create_ondemand_singleton('mylibrary.Threadpool', MyThreadPoolClass).

Leave a Reply

Your email address will not be published. Required fields are marked *