Redis definition in python and pythonredis Definition
Redis official introduction, the redis client of python can be defined in one place and used everywhere. Is that true? Is it enough to define it once in the configuration file?
The following code is used:
1 import redis 2 import threading 3 4 redis_store = redis.Redis(host='127.0.0.1', port=6380, db=0, password='dahai123') 5 6 7 def test_redis_fun(x): 8 redis_store.set(x, '123456') 9 10 11 def test_redis():12 threads = [threading.Thread(target=test_redis_fun, args=(i, )) for i in range(1, 100)]13 [t.start() for t in threads]14 [t.join() for t in threads]15 16 17 def cat_result():18 all_numbers = [redis_store.get(x) for x in range(1, 100)]19 return all_numbers20 21 if __name__ == '__main__':22 redis_store.flushdb()23 test_redis()24 result = cat_result()25 print result
This is a multi-threaded connection for redis. The result is as follows:
['123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123 ', '123', '123', '123', '123', '123', '123', '123', '123', '123', '123']
127.0.0.1:6380> auth dahai123OK127.0.0.1:6380> keys * 1) "87" 2) "34" 3) "97" 4) "95" 5) "92" 6) "30" 7) "13" 8) "99" 9) "53"10) "63"11) "37"12) "48"13) "10"14) "93"15) "75"16) "66"17) "60"18) "72"19) "36"20) "58"21) "38"22) "42"23) "78"24) "31"25) "29"26) "59"27) "61"28) "6"29) "89"30) "69"31) "16"32) "17"33) "33"34) "21"35) "62"36) "50"37) "81"38) "49"39) "56"40) "65"41) "4"42) "73"43) "28"44) "94"45) "86"46) "25"47) "2"48) "70"49) "85"50) "46"51) "88"52) "14"53) "23"54) "45"55) "83"56) "1"57) "18"58) "39"59) "3"60) "55"61) "74"62) "20"63) "8"64) "96"65) "24"66) "54"67) "76"68) "90"69) "15"70) "40"71) "9"72) "57"73) "91"74) "80"75) "32"76) "41"77) "11"78) "79"79) "22"80) "7"81) "98"82) "84"83) "12"84) "64"85) "68"86) "71"87) "35"88) "77"89) "44"90) "82"91) "5"92) "52"93) "19"94) "27"95) "43"96) "51"97) "26"98) "47"99) "67"
The result should be correct. It seems that the official website is correct. redis clients can be used everywhere as long as they are defined. They can also be defined in the configuration file in the future.