Hi,
I am using the following pattern to use my libraries with web2py: some of
my utilities are creating connections to databases where I have data that I
need to serve with web2py. This data is not modeled with web2py models. In
order to avoid re-creating these connections with every single request (I
have a site which is performing requests with a 1s period), I have
discovered that the importing of modules is not cleaning the module global
variables. So now what I am doing is creating a cache for the objects that
I want to be persistent across requests. This is my code, in file
subscribers.py:
class SubscriberPoolCls:
def __init__(self):
self.subscriber_pool = { }
def get_subscriber(self, subsriber_id, myorg):
log.info('get_subscriber > Requested subscriber_id=%s myorg=%d' % (
subsriber_id, myorg))
if not subsriber_id in self.subscriber_pool:
self.subscriber_pool[subsriber_id] = Subscriber(myorg,subsriber_id
)
return self.subscriber_pool[subsriber_id]
def unsubscribe_all(self):
for subsriber_id in self.subscriber_pool:
self.subscriber_pool[subsriber_id].unsubscribe()
_subscriber_pool = None
def SubscriberPool():
global _subscriber_pool
if not _subscriber_pool:
_subscriber_pool = SubscriberPoolCls()
return _subscriber_pool
And then in a request I do the following (default.py):
from subscribers import SubscriberPool
subscriber_pool = SubscriberPool()
...
def init_session():
if not session.my_session_id:
session.my_session_id = get_uuid()
...
@auth.requires_login()
@service.jsonrpc
def get_call_details(pars_json):
init_session()
myorg = session.auth.user.org_id
pars = simplejson.loads(pars_json)
subscriber = subscriber_pool.get_subscriber(session.my_session_id, myorg
)
activity_cdr = subscriber.get_call_details(pars['cdr_doc_id'])
response = {
'cdr_details' : activity_cdr,
}
return simplejson.dumps(response)
By doing this I can create a subscriber object associated to the session
and the organization, and I get to reuse this object in subsequent requests.
Now I have the following questions:
1. Why are the imported modules keeping the global variables? default.py
is not, as far as I can tell. I would say it is reparsed with each request.
2. I have the problem that my object cache
(SubscriberPoolCls.subscriber_pool) can grow indefinitely. I do not know
how or when to delete entries from this cache.
3. Do you think this pattern is dangerous? Do you have an alternative?
Thanks,
Daniel