python - Globally available object in django for frequent acess -
i'm relatively new django & learning bit-by-bit. i've subscribed service, uses dll provide data need.
required data service, have make few initializations; code takes around 2-3 seconds (requires dispatching dll, , making init call follows:)
from win32com.client import dispatch #pythoncom.coinitialize() zk = dispatch("easyconnect.serverside") print(zk.cmdinit(my_id, my_pass, server_ip))
i need zk object available globally, other modules not have perform init ambiguously , speed data access process.
i tried django's caching framework, helps caching entire site, or web pages, think it's not want. also, putting zk cache returns me error states cannot put cache.
what alternatives address problem?
yes can define client globally , access in other modules, problem client initialized app loaded not want connection time during app loading want start client when need , once hence need django.utils.functional.lazyobject (a wrapper class can used delay instantiation of wrapped class)
clients.py (lets create lazy client)
from django.utils.functional import lazyobject django.conf import settings win32com.client import dispatch class easyconnect: def __init__(self): self.easy_connect = dispatch('easyconnect.serverside') self.easy_connect.cmdinit( settings.eassyconnect_id, settings.eassyconnect_pass, settings.eassyconnect_ip ) class easyconnectclient(lazyobject): def _setup(self): self._wrapped = easyconnect() client = easyconnectclient()
now access client other modules:
from clients import client client.easy_connect.make_request() # make_request dummy method, use actual method
Comments
Post a Comment