Skip to content
This repository was archived by the owner on Jan 9, 2024. It is now read-only.
This repository was archived by the owner on Jan 9, 2024. It is now read-only.

MultiThreading Problem about theadingsafety #397

@Jackvie

Description

@Jackvie

redis-cluster-py version 2.1.0 redis 3.5.1

  1. 1000 client objects created by default class Redis used 0.0001 second, but when I used RedisCluster, It took 28 seconds
  2. So the project i create only one client object, and i use this object no matter where I get the value in code, by testing. After testing, this object is thread safe
  3. The problem is that when I use the pipeline created by the fastest unique object, there is a problem with the value in multiple threads

code like this :

from django.conf import settings
from concurrent.futures import ThreadPoolExecutor
client = settings.READONLYCONST.r_client
print(client)
pipe = client.pipeline()

def func(task_id):
    try:
        if task_id % 2 ==0:
            info = client.hget('aaabbb', 1)
            print('============OK==========', info)
        else:
            pipe.setex('a', 60, 'bbb')
            pipe.setex('b', 60, 'bbb')
            pipe.execute()
    except Exception as e:
        print(task_id, e, '===')


def test_redis():
    print('BEGIN')
    with ThreadPoolExecutor(100) as executor:
        for i in range(100):
            executor.submit(func, i)
    print('END')

test_redis()

result in console like this:

RedisCluster<172.18.10.8:7101, 172.18.10.8:7102, 172.18.10.8:7103>
BEGIN
============OK========== None
============OK========== None
============OK========== OK
============OK========== None
============OK========== OK
============OK========== None
============OK========== None
END

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions