Hi, I've been using web.py 0.3 and I've gotten quite a bit of help
just lurking here, but I think I'm stuck. I've been using MongoDB and
[webpy mongodb sessions], and I think I've discovered a pymongo
connection leak that happens if a user's session times out.

To try and isolate this, I wrote this (to be used in conjunction with
[commit 3e14669]:

------------

import web
import pymongo

from session import MongoStore
from pymongo import Connection
import users

c = Connection()
db = c.players

web.config.session_parameters['timeout'] = 15 # yes, 15-second timeout

class Index:
    def GET(self):
        return "A page."

urls = ('/', Index)

app = web.application(urls, globals(), False)

session = web.session.Session(app, MongoStore(db, 'sessions'))

users.session = session
users.collection = db.players

if __name__ == '__main__':
    app.run()

---------------

(this snippet also at https://gist.github.com/780723 )

I plopped this (as code.py) in a directory with session.py and
users.py and a directory named mongo.db. I then started up mongod and
then code.py to handle initial database and collection creation.

After qutting and restarting both mongod and web.py, I then see this
entry in mongod's output:

Fri Jan 14 22:12:00 [initandlisten] connection accepted from
127.0.0.1:58967 #1

OK, that's the one connection. Let's visit http://localhost:8080/ and
see what happens.

Fri Jan 14 22:14:24 [initandlisten] connection accepted from
127.0.0.1:58996 #2

Ooo-kay, a second connection. Let's reload http://localhost:8080/ ten
times, a couple seconds apart. (keep in mind that the session
timeout's set to 15 seconds.)

Fri Jan 14 22:15:47 [initandlisten] connection accepted from
127.0.0.1:59015 #3

OK, one more connection for the batch of ten. None have been released
yet.

Let's try reloading six times, each with 16 seconds or so between
them.

Fri Jan 14 22:16:45 [initandlisten] connection accepted from
127.0.0.1:59031 #4
Fri Jan 14 22:16:56 [initandlisten] connection accepted from
127.0.0.1:59037 #5
Fri Jan 14 22:17:14 [initandlisten] connection accepted from
127.0.0.1:59046 #6
Fri Jan 14 22:17:29 [initandlisten] connection accepted from
127.0.0.1:59053 #7
Fri Jan 14 22:17:45 [initandlisten] connection accepted from
127.0.0.1:59058 #8
Fri Jan 14 22:18:02 [initandlisten] connection accepted from
127.0.0.1:59064 #9

One connection accepted for each page reload, and no "[connN] end
connection …" messages that I'm used to seeing when I access the
database and call cxn.disconnect() (with a context manager) right
afterwards.

Since I don't think I'll be able to dig into the runloop and wrap all
DB-using functions with a `with disconnecting(pymongo.Connection()) as
cxn:` block, where should I set up and clean up the pymongo.Connection
object with the db in this line?

session = web.session.Session(app, MongoStore(db, 'sessions'))


I suppose I ought to point out that I don't _think_ I *need* sessions;
I'm writing a game, and there's nothing I need connection tracking for
until someone logs in. Once they're in, of course, I expect to use
multiple browser windows and fancypants AJAX to do things, though.



[webpy mongodb sessions]: https://github.com/whilefalse/webpy-mongodb-sessions
[commit 3e14669]: 
https://github.com/whilefalse/webpy-mongodb-sessions/commit/3e14669

-- 
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/webpy?hl=en.

Reply via email to