Prevent creating of expensive object on each request

81 Views Asked by At

I have an object which loads all the data from DB Object_X.

This object has a few methods defined. I pass some parameter and based on parameter I call one of the functions in Object_X, it uses pre-populated data in object and parameter to get some result.

I have created a web service which invokes any method defined in Object_X and returns a result.

My problem is that for every request I am loading all the data from db again and again which is time consuming. Is there a way that I can load the data one time when I start a server and uses the same object for each subsequent request?

2

There are 2 best solutions below

0
On

I suppose you are using web.py frame work. And it will base on how you use web.py. For simplest way, use included CherryPy WSGI server, you can try this:

import web

urls = (
  '/', 'index'
)

def inc_num():
    cnt = 0
    while True:
        cnt += 1
        yield cnt


class index:
    num = inc_num()
    def GET(self):
        return "Hello, world! %s" % self.num.next()


if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

Every time when you visit

http://localhost:8080/

You will see

Hello, world! 1
Hello, world! 2
...

The number keep increasing every time you visit it. Which means server remember the status, also means inc_num only start once.

0
On

Assuming your application is served by a long running process (as opposed to something like plain CGI), AND your database is never updated, you always have the option to instantiate your Object_X at the module's top-level (making it a global), so it will only be created once per process. Now it means that you'll have some overhead on process start, and that you'll have to restart all your processes everytime your db is updated.

The real question IMHO is why do you want to load all your db right from the start ? If that's for "optimization" then there might be better tools and strategies (faster db, caches etc).