Changeset 1922
- Timestamp:
- 03/14/08 10:18:00
- Files:
-
- trunk/cherrypy/lib/sessions.py (modified) (4 diffs)
- trunk/cherrypy/test/test_session.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/lib/sessions.py
r1904 r1922 463 463 class MemcachedSession(Session): 464 464 465 # The most popular memcached client for Python isn't thread-safe. 466 # Wrap all .get and .set operations in a single lock. 467 mc_lock = threading.RLock() 468 469 # This is a seperate set of locks per session id. 465 470 locks = {} 471 466 472 servers = ['127.0.0.1:11211'] 467 473 … … 480 486 481 487 def _load(self): 482 return self.cache.get(self.id) 488 self.mc_lock.acquire() 489 try: 490 return self.cache.get(self.id) 491 finally: 492 self.mc_lock.release() 483 493 484 494 def _save(self, expiration_time): 485 495 # Send the expiration time as "Unix time" (seconds since 1/1/1970) 486 zeroday = datetime.datetime(1970, 1, 1, tzinfo=expiration_time.tzinfo) 487 td = expiration_time - zeroday 488 td = (td.days * 86400) + td.seconds 489 if not self.cache.set(self.id, (self._data, expiration_time), td): 490 raise AssertionError("Session data for id %r not set." % self.id) 496 td = int(time.mktime(expiration_time.timetuple())) 497 self.mc_lock.acquire() 498 try: 499 if not self.cache.set(self.id, (self._data, expiration_time), td): 500 raise AssertionError("Session data for id %r not set." % self.id) 501 finally: 502 self.mc_lock.release() 491 503 492 504 def _delete(self): … … 502 514 self.locks[self.id].release() 503 515 self.locked = False 516 517 def __len__(self): 518 """Return the number of active sessions.""" 519 raise NotImplementedError 504 520 505 521 … … 532 548 """Close the session object for this request.""" 533 549 sess = getattr(cherrypy.serving, "session", None) 534 if sess and sess.locked:550 if getattr(sess, "locked", False): 535 551 # If the session is still locked we release the lock 536 552 sess.release_lock() trunk/cherrypy/test/test_session.py
r1915 r1922 116 116 117 117 def test_0_Session(self): 118 self.getPage('/setsessiontype/ram') 118 119 self.getPage('/clear') 119 120 … … 189 190 190 191 data_dict = {} 192 errors = [] 191 193 192 194 def request(index): … … 201 203 c.endheaders() 202 204 response = c.getresponse() 203 self.assertEqual(response.status, 200)204 205 body = response.read() 205 self.failUnless(body.isdigit()) 206 if response.status != 200 or not body.isdigit(): 207 errors.append((response.status, body)) 208 else: 209 data_dict[index] = max(data_dict[index], int(body)) 206 210 # Uncomment the following line to prove threads overlap. 207 211 ## print index, 208 data_dict[index] = max(data_dict[index], int(body))209 212 210 213 # Start <request_count> requests from each of … … 222 225 hitcount = max(data_dict.values()) 223 226 expected = 1 + (client_thread_count * request_count) 227 228 for e in errors: 229 print e 224 230 self.assertEqual(hitcount, expected) 225 231 … … 310 316 self.getPage('/testStr', self.cookies) 311 317 self.assertBody('3') 318 self.getPage('/length', self.cookies) 319 self.assertErrorPage(500) 320 self.assertInBody("NotImplementedError") 312 321 self.getPage('/delkey?key=counter', self.cookies) 313 322 self.assertStatus(200) … … 382 391 383 392 384 385 393 if __name__ == "__main__": 386 394 setup_server()

