Setting request variables with engine.subscribe

34 views
Skip to first unread message

Voltron

unread,
Dec 2, 2009, 4:36:39 AM12/2/09
to cherrypy-users
I am creating a pooled database connection using this:

# Very simplified, without try conditionals and so on

def get_mongodb():
connection = Connection("localhost", cherrypy.config.get
(db_default.port'), pool_size=5, timeout=-1)
cherrypy.request.db = connection[cherrypy.config.get
('production.db')]


cherrypy.engine.subscribe('start', get_db)

This makes the connection and stores the db in the request variable,
the only problem is, my applications error out with a traceback
stating that cherrypy.request.db does not exist.

How can I store a shared, pooled and thread safe db connection for all
my controllers in a request variable?


Thanks

João Pinto

unread,
Dec 2, 2009, 4:53:18 AM12/2/09
to cherryp...@googlegroups.com
Hello Voltron,
I believe you need to define the pool at the request level, using a tool.
Take a look at:
http://code.google.com/p/webpyte/source/browse/trunk/webpyte/sqlalchemy_tool.py

Best regards,

2009/12/2 Voltron <nhy...@googlemail.com>

--

You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherryp...@googlegroups.com.
To unsubscribe from this group, send email to cherrypy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en.





--
João Pinto
IRC: joaopinto @ irc.freenode.net
Jabber ID: lamego...@gmail.com
GetDeb Team Leader - http://www.getdeb.net

Voltron

unread,
Dec 2, 2009, 5:09:19 AM12/2/09
to cherrypy-users
Hi Joao!

Thanks for the pointer. I have coded a custom tool thet works very
well, the thing is, a new connection is created with every new
request, I want to create ther connection once and pass it to every
request so that its shared.

Thanks

On Dec 2, 10:53 am, João Pinto <lamego.pi...@gmail.com> wrote:
> Hello Voltron,
> I believe you need to define the pool at the request level, using a tool.
> Take a look at:http://code.google.com/p/webpyte/source/browse/trunk/webpyte/sqlalche...
>
> Best regards,
>
> 2009/12/2 Voltron <nhy...@googlemail.com>
>
>
>
> > I am creating a pooled database connection  using this:
>
> > # Very simplified, without try conditionals and so on
>
> > def get_mongodb():
> >    connection = Connection("localhost", cherrypy.config.get
> > (db_default.port'), pool_size=5, timeout=-1)
> >    cherrypy.request.db =  connection[cherrypy.config.get
> > ('production.db')]
>
> > cherrypy.engine.subscribe('start', get_db)
>
> > This makes the connection and stores the db in the request variable,
> > the only problem is, my applications error out with a traceback
> > stating that cherrypy.request.db does not exist.
>
> > How can I store a shared, pooled and thread safe db connection for all
> > my controllers in a request variable?
>
> > Thanks
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "cherrypy-users" group.
> > To post to this group, send email to cherryp...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > cherrypy-user...@googlegroups.com<cherrypy-users%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/cherrypy-users?hl=en.
>
> --
> João Pinto
> IRC: joaopinto @ irc.freenode.net
> Jabber ID: lamego.pi...@gmail.com
> GetDeb Team Leader -http://www.getdeb.net

Robert Brewer

unread,
Dec 2, 2009, 11:03:12 AM12/2/09
to cherryp...@googlegroups.com
You can't. Stick it on the Application object instead:

app = tree.mount(...)
app.db = get_mongo_db()

Then your controllers can access it via cherrypy.request.app.db.

But, *only* do so if that 'db' object is "threadsafe" (in the sense that
multiple threads can use it simultaneously), which I rather doubt. If it
isn't threadsafe in that sense, it might be "threadsafe" in the sense
that you need one per thread. In that case, subscribe your get_mongo_db
callback to the 'start_thread' channel instead of the 'start' channel,
and stick the 'db' object on cherrypy.thread_data (a threadlocal)
instead.


Robert Brewer
fuma...@aminus.org

Sylvain Hellegouarch

unread,
Dec 2, 2009, 8:45:11 AM12/2/09
to cherryp...@googlegroups.com
You need both a plugin and a tool. The plugin isn't called on a per
request basis.

Try something like:


import cherrypy
from cherrypy.process import plugins

class SomeConnectionClass(object): pass

class DoItOnce(plugins.SimplePlugin):
def start(self):
self.bus.cx = SomeConnectionClass()

def stop(self):
delattr(self.bus, 'cx')

def attach_cx(debug=False):
cherrypy.request.cx = cherrypy.engine.cx

cherrypy.tools.attachconn = cherrypy.Tool("before_handler", attach_cx)

class Root(object):
@cherrypy.expose
def index(self):
print cherrypy.request.cx

if __name__ == '__main__':
DoItOnce(cherrypy.engine).subscribe()
cherrypy.quickstart(Root(), '/', {'/': {'tools.attachconn.on': True}})


--
Sylvain Hellegouarch
http://www.defuze.org

Voltron

unread,
Dec 2, 2009, 5:06:59 PM12/2/09
to cherrypy-users
Great answers. Thanks guys

Voltron

unread,
Dec 2, 2009, 6:16:26 PM12/2/09
to cherrypy-users
Oh! Before I forget. One very, last thing :) I am using sessions. It
works well. I subclassed the cherrypy sessions object according to the
examples on the site. This subclass creates its own db/connection, I
would rather that it uses the same as the main application itself,
should I pass it to the session tool in:

def attach_cx(debug=False):
cherrypy.config['tools.sessions.db'] = cherrypy.engine.cx
cherrypy.request.cx = cherrypy.engine.cx


or

'tools.sessions.port' : 27017,
'tools.sessions.host' : '127.0.0.1',
'tools.sessions.db_name' : 'test_db',
'tools.sessions.db' : cherrypy.request.app.db


Thanks

Robert Brewer

unread,
Dec 2, 2009, 8:14:16 PM12/2/09
to cherryp...@googlegroups.com
Neither. Hooks and Tools have access to the same objects that your page
handlers do, so in your session tool, just "import cherrypy" and use
cherrypy.request.app.db.


Robert Brewer
fuma...@aminus.org

Voltron

unread,
Dec 3, 2009, 7:27:14 AM12/3/09
to cherrypy-users
GREAT!

Thanks guys!
Reply all
Reply to author
Forward
0 new messages