class RedisClient(object):
def __init__(self):
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
self.client = redis.StrictRedis(connection_pool=pool)
Write a redis client with a connection pool according to the documentation, and then generate an instance for global use.
It has been tested to be normal to share an instance in multiple threads.
But in the case of multiple processes, the test failed
class ProcessRdeisTest(Process):
def __init__(self,client):
self._client = client
Written like this, when executing start, an error will be reported and serialization cannot be performed.
changed to:
class ProcessRdeisTest(Process):
def __init__(self):
pass
def run(self):
self._client = RedisClient()
while Ture:
dosomething()
It works like this, but is this connection method correct? Is there a better way to achieve this?
In the main thread directly
process1 = ProcessRdeisTest('p1')
process1.start()
Called in this way
Owner, python redis has its own connection pool: