import pyssdb import random import time c = pyssdb.Client('192.168.1.250',8888) chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz' digits = '0123456789' def random_generate_string(length): return ''.join(random.sample(chars, length)) def random_generate_number(length): if length > len(digits): digit_list = random.sample(digits, len(digits)) digit_list.append(random.choice(digits)) return ''.join(digit_list) return ''.join(random.sample(digits, length)) def random_generate_data(num): c = [num] phone_num_seed = 13100000000 def _random_generate_data(): c[0] += 1 return ( c[0], "last_name_" + str(random.randrange(100000)), "first_name_" + str(random.randrange(100000)), random.choice('MF'), random.randint(1, 120), phone_num_seed + c[0], random_generate_string(20), random_generate_string(10), time.strftime("%Y-%m-%d %H:%M:%S") ) return _random_generate_data #MULTI_HSET batch_count = 0 batch_size = 100 begin_time = time.time() loop_count=1000 all_count=0 for x in range(loop_count): batch_count = x * batch_size all_count=all_count+batch_size gen_fun = random_generate_data(batch_count) batch_data = [gen_fun() for x in range(batch_size)] for a in batch_data : #成功写入数据量:100000个! #总共耗时(s): 63.21674847602844 c.multi_hset('person_' + str(a[0]), 'last_name', a[1],'first_name',a[2],'Sex',a[3],'Age',a[4],'Tel',a[5],'Memo',a[6],'Date',a[7]) #print(c.hgetall('person_'+ str(a[0]))) print('成功写入数据量:'+str(all_count)+"个!") end_time = time.time() total_sec = end_time - begin_time print("总共耗时(s): " + str(total_sec)) c.disconnect() print('已成功断开链接!')
写在最后:
1、不要过份追求连接池,因为我们的PYTHON一般当脚本使用,不涉及到高并发,多线程,不需要连接池,只有一个连接,连续操作而以,以前的想法是误区,需要高并发使用LUA去!
2、感觉速度还是可以接受的,使用PYTHON就是因为它方便,能快速实现想法,这就够了!