Re: RoutesDispatches causing Controller Class init to execute twice
Johan Hartzenberg <[email protected]> Sat, 5 Nov 2016 13:04:56 -0700 (PDT)
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <[email protected]> |
I ended up moving my setup_routes function into the app class as a method.
Here I now refer "self" instead of a new instance of "Echo_v1". This
works and my app does not end up having double the open connections to the
database any more.
On Thursday, November 3, 2016 at 11:36:55 PM UTC+2, Johan Hartzenberg wrote:
>
> I have always wondered about the need to specify the controller in the
> dispatches connect and in the tree.mount call.
>
> It is a bit of a pain since I initiate a pool of DB connections in my app
> class.
>
> So in the main program I have
>
> import cherrypy
> from routehandlers import Echo_v1, setup_routes
>
>
>
> if __name__ == '__main__':
> general_conf = {'/': {'tools.gzip.on': True,
> 'tools.trailing_slash.on': False,
> 'log.screen': False}}
> api_routes_conf = {}
> api_routes_conf.update(dict(general_conf))
> api_routes_conf['/'].update({'request.dispatch': setup_routes()})
>
> cherrypy.tree.mount(Echo_v1(), "/v1/", api_routes_conf)
> cherrypy.engine.start()
> cherrypy.engine.block()
>
> And in the class def I have
>
>
>
>
> import cherrypy
>
> *# [...snip...]*
>
> class Echo_v1(object):
>
> def __init__(self):
> dbname, dbuser, dbpass, dbhost = get_db_parameters()
> poolsize = cherrypy.config.get("db_conn_poolsize")
> self.dbpool = ConnectionPool(dbname, dbuser, dbpass, dbhost, poolsize)
> print("Created pool with %d Open DB connections" % self.dbpool.get_poolsize())
>
>
> # [ ... snip ...]
>
> def setup_routes():
> testCntrl = Echo_v1()
> d = cherrypy.dispatch.RoutesDispatcher()
> d.connect(name='echoback_route', route='/echoback',
> controller=testCntrl,
> action='echoaction')
> d.connect(name='index_route', route='/x',
> controller=testCntrl,
> action='index')
>
>
>
> It works.... but the debug message appears twice and I see double the
> expected nr of open connections on the database.
>
> So clearly the class __init__ runs twice, once in setup_routes and once in
> the main program when mounting the app.
>
> I'm thinking I could create a class instance in the main and pass that to
> setup_routes and to the tree.mount call
>
> But maybe setup_routes aught to be a special method in the app class. And
> maybe I can have it update the app config all in one go?
>
> Suggestions please!
>
--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.