from queue import Queue
import random
import threading
import time
from mysql_client import MysqlManager
mysql_config ={
"database": "xiaoyuteacher",
"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "1qaz2wsx"
}
# 生成者线程,压测中是从数据库查数据的角色
class Producer(threading.Thread):
def __init__(self, t_name, queue,original_data): # 调用父线程的构造方法。
threading.Thread.__init__(self, name = t_name)
#队列用来存储生成的数据,consumer会用
self.data = queue
#这是第一步生成的original_data
self.original_data = original_data
def run(self):
for i in self.original_data:
account = MysqlManager(**mysql_config)
try:
mobile = account.execute_return_result("select mobile from index_user where id={}".format(i))[0]["mobile"]
name = account.execute_return_result("select name from index_user where mobile={}".format(mobile))[0]["name"]
except Exception as e:
print(e)
return ""
print("producer在制造数据ing",[mobile,name])
self.data.put([mobile,name])
account.close()
print("%s: %s finished!" % (time.ctime(), self.getName()))
# 消费者线程
class Consumer(threading.Thread):
def __init__(self, t_name, queue):
threading.Thread.__init__(self, name=t_name)
self.data = queue
def run(self):
for i in range(90):
lock.acquire()
val = self.data.get()
user_info=[val[0],val[1]]
lock.release()
lock.acquire()
#此处应该根据userinfo进行接口请求,返回token,增加到result_list里,我先省略了
print(user_info)
result_list.append("success")
lock.release()
print("result_list", result_list,len(result_list))
print("%s: %s finished!" % (time.ctime(), self.getName()))
def get_original_data(filepath):
original_data = []
with open(filepath) as fp:
data = fp.readlines()
for i in data:
original_data.append(str(i.strip()))
return original_data
if __name__ == '__main__':
#main()
#第一步:获取原始数据account_id,获取
original_data = get_original_data("/Users/yu.jing/Documents/my_tester/utils/1.json")
print(original_data)
#第二步:producer的作用:根据account_id 去account数据库取对应的mobile,name,并存进queue,共consumer使用
queue = Queue() # 创建一个队列对象(特点先进先出)
result_list = []
producer = Producer('Pro.', queue,original_data) # 生产者对象
#循环取queue里mobile,name,进行接口请求,返回token,存到result_list,得到结果
consumer = Consumer('Con.', queue) # 消费者对象
lock = threading.Lock()
producer.start()
consumer.start()
producer.join()
consumer.join()
print('All threads terminate!')