【发布时间】:2020-06-24 22:41:12
【问题描述】:
我对此有两个问题。
创建一个全局实例并在每个线程中重用还是在每个线程中创建一个新实例?
-
使用
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool)或
r = redis.StrictRedis(host='localhost', port=6379, db=0)
关于 ConnectionPool 的文档说:
You may choose to do this in order to implement client side sharding or have finer grain control of how connections are managed。但我不明白client side sharing指的是什么。
更新
如果使用 ConnectionPool,下面哪种方式是正确的?
答:
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 .....
乙:
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 .....
【问题讨论】:
标签: python multithreading redis