diff options
author | Andy McCurdy <andy@andymccurdy.com> | 2010-03-05 17:15:28 -0800 |
---|---|---|
committer | Andy McCurdy <andy@andymccurdy.com> | 2010-03-05 17:15:28 -0800 |
commit | c83542e58593fd0ce6347f399b65763abfebcccb (patch) | |
tree | d7191bced5b8ad8395406f2f30370e0c6bb1f759 | |
parent | 800720c7bd07c31511239df27d512e98d757e945 (diff) | |
download | redis-py-c83542e58593fd0ce6347f399b65763abfebcccb.tar.gz |
the Redis class is now a thread local. This means you can safely pass it around between threads and connections will get swapped out correctly.
-rw-r--r-- | redis/client.py | 2 | ||||
-rw-r--r-- | tests/connection_pool.py | 27 |
2 files changed, 27 insertions, 2 deletions
diff --git a/redis/client.py b/redis/client.py index fe38909..a2897d9 100644 --- a/redis/client.py +++ b/redis/client.py @@ -143,7 +143,7 @@ def zset_score_pairs(response, **options): return zip(response[::2], map(float, response[1::2])) -class Redis(object): +class Redis(threading.local): """ Implementation of the Redis protocol. diff --git a/tests/connection_pool.py b/tests/connection_pool.py index 133bec9..681b46c 100644 --- a/tests/connection_pool.py +++ b/tests/connection_pool.py @@ -1,4 +1,6 @@ import redis +import threading +import time import unittest class ConnectionPoolTestCase(unittest.TestCase): @@ -17,4 +19,27 @@ class ConnectionPoolTestCase(unittest.TestCase): r2.select('localhost', 6379, db=9) self.assertEquals(r1.connection, r2.connection) -
\ No newline at end of file + def test_threaded_workers(self): + r = redis.Redis(host='localhost', port=6379, db=9) + r.set('a', 'foo') + r.set('b', 'bar') + + def _info_worker(): + for i in range(50): + _ = r.info() + time.sleep(0.01) + + def _keys_worker(): + for i in range(50): + _ = r.keys() + time.sleep(0.01) + + t1 = threading.Thread(target=_info_worker) + t2 = threading.Thread(target=_keys_worker) + t1.start() + t2.start() + + for i in [t1, t2]: + i.join() + +
\ No newline at end of file |