简体   繁体   中英

How to be more efficient using redis-py in multithreading program?

I have two questions about this.

  1. create a global instance and reuse in each thread or create a new instance in each thread?

  2. use

    pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
    r = redis.Redis(connection_pool=pool)

    or

    r = redis.StrictRedis(host='localhost', port=6379, db=0)

    document says about ConnectionPool: You may choose to do this in order to implement client side sharding or have finer grain control of how connections are managed .But I can't understand what is client side sharing refer to .

update
if use ConnectionPool, which way below is right ?
A:

pool = redis.ConnectionPool(host='localhost', port=6379, db=0)

class DownloadThread(threading.Thread):
    def __init__(self,pool):
        threading.Thread.__init__(self)
        self.r = redis.Redis(connection_pool=pool)
    def run(self):
        while True:
            self.r .....

B:

pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool)

class DownloadThread(threading.Thread):
    def __init__(self,r):
        threading.Thread.__init__(self)
        self.r = r
    def run(self):
        while True:
            self.r .....

Well, there are a couple questions here, so I will attempt to answer each separately.

  1. If you are going to use threads, then you certainly want individual connections. Redis connections will not be thread safe if shared as you could send a request on one thread and read on another potentially (unless you use a mutex / lock). The connection pool implementation appears to me to be thread safe, so you should opt for that, or create your own connection pool using something like a queue.Queue where the individual connections are placed and threads get and put onto the queue.

  2. Client side sharding is the "poor man's" way to shard data on multiple redis instances. A typical strategy involves applying some sort of hash, like a crc32 to the target key and the taking the modulus of that value and the number of shards. An example would look something like this:

--

>>> binascii.crc32("foo") % 3
1
>>> binascii.crc32("bar") % 3
2
>>> binascii.crc32("baz") % 3
0

This is assuming we have 3 shards (or individual redis server instances). The key foo lives in the 2nd shard (index 1). The key bar lives on the 3rd shard (index 2) and baz is on the 1st shard.

Redis Cluster (in beta of Redis 3.0.0) aims to do the sharding server side vs client side.

Redis is now thread-safe: https://github.com/andymccurdy/redis-py

Redis client instances can safely be shared between threads. Internally, connection instances are only retrieved from the connection pool during command execution, and returned to the pool directly after. Command execution never modifies state on the client instance.

Connection pool is automatically created by each instance of Redis()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM