Re: how to build up a pyramid resource tree for a traversal based application that is available during whole application runtime

107 views
Skip to first unread message

Wyatt Baldwin

unread,
Jan 15, 2013, 12:21:30 PM1/15/13
to pylons-...@googlegroups.com
On Tuesday, January 15, 2013 4:39:50 AM UTC-8, Sascha Gottfried wrote:
Hello group,
I am looking for a way to build up and keep my domain model in memory during pyramid serves my application. This time I do not want to use a SQL database or ZODB as the persistence layer. I currently maintain an URL dispatch based pyramid app using ZODB - this is where my current pyramid experience is coming from.

Now I want to use pyramid to build an application to visualize data loaded from large CSV files on local harddisk. I use traversal to map URLs to folders and files. During application startup I create a basic resource tree while using several plain python model classes. Typical application usage should enlarge the resource tree by lazy loading child objects as requested by users. Lazy loading takes several seconds. This should happen only one time for every "resource" in the resource tree. My intention is to keep the main application object(a.k.a resource tree) in memory during several requests. Parsing CSV files during every request is way to slow. This actually happens.

As request by traversal based applications I do pass the root_factory to the Configurator. The pyramid glossary cleary notes: "The “root factory” of a Pyramid application is called on every request sent to the application.". This way the application restarts building the resource tree on every request. 

def main(global_config, **settings):
    root = settings['root']
    if root is None:
        raise ValueError('virginia requires a root')

    def get_root(environ):     
        fs = Filesystem(os.path.abspath(os.path.normpath(root)))
        directory = Directory(fs, root) 
        application = Application(directory) 
        return application
    
    config = Configurator(root_factory=get_root, settings=settings)

I already tried passing a singleton to the root_factory. I currently do not use any caching/sessioning like beaker. 
Any suggestions how to build up a resource tree at runtime that keeps alive during requests??? Thanks for your answers!

Create the root object *outside* of `get_root()` and simply have `get_root()` return that object.

Sascha Gottfried

unread,
Jan 16, 2013, 4:26:28 AM1/16/13
to pylons-...@googlegroups.com
That makes the difference. Thank you!
Reply all
Reply to author
Forward
0 new messages