X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Fsession.py;h=d088ad7387d60dd9c8cc209c7ca44a19ca20e8ac;hb=fed807e31c96fef8d941b34a25a1f5044b2d4e8d;hp=513725d12d039dff43959fd28e95182438ea2343;hpb=b409a33843abb3221edd27016558c39cf33a6510;p=wrw.git diff --git a/wrw/session.py b/wrw/session.py index 513725d..d088ad7 100644 --- a/wrw/session.py +++ b/wrw/session.py @@ -1,5 +1,5 @@ import threading, time, pickle, random, os -import cookie +from . import cookie __all__ = ["db", "get"] @@ -76,7 +76,6 @@ class db(object): self.cookiename = cookiename self.path = path self.lock = threading.Lock() - self.lastuse = 0 self.cthread = None self.freezetime = 3600 @@ -99,18 +98,17 @@ class db(object): def cleanloop(self): try: - lastuse = self.lastuse - while self.lastuse >= lastuse: - lastuse = self.lastuse + while True: time.sleep(300) self.clean() + if len(self.live) == 0: + break finally: with self.lock: self.cthread = None def fetch(self, req): now = int(time.time()) - self.lastuse = now sessid = cookie.get(req, self.cookiename) with self.lock: if self.cthread is None: @@ -131,18 +129,17 @@ class db(object): except KeyError: sess = session() self.live[sess.id] = sess - req.oncommit(self.addcookie) + sess.new = True req.oncommit(self.ckfreeze) return sess - def addcookie(self, req): - sess = req.item(self.fetch) - cookie.add(req, self.cookiename, sess.id, self.path) - def ckfreeze(self, req): sess = req.item(self.fetch) if sess.dirty(): try: + if getattr(sess, "new", False): + cookie.add(req, self.cookiename, sess.id, self.path) + del sess.new self.freeze(sess) except: pass