数据库准备至少15个学生的信息,测试数据文件中准备30个学生的ID;[1,2,3,4,。。。。。30]
针对Student进行查询操作,调用selectStudent方法,采用5个子线程进行处理
待查询的学生ID存放在列表中,5个子线程各自分配一批学生ID进行查询操作,
第一个子线程:获取ID%5==1的ID号
第二个子线程:获取ID%5==2的ID号
第三个子线程:获取ID%5==3的ID号
第四个子线程:获取ID%5==4的ID号
第五个子线程:获取ID%5==0的ID号
如果查询到相应的结果,将学生ID记录到一个公共的列表中,没有查询到的学生ID记录到另外一个公共的列表中
屏幕打印最终的查询结果情况。
# foundList=[] notFoundList=[]_ ```python import threading class DBOperation: @staticmethod def openDB(host, user, pwd, port, db):import pymysql
dbObj = pymysql.connect(host=host, user=user, password=pwd, port=int(port), db=db, charset='utf8')
curObj = dbObj.cursor() # 生成游标对象
return dbObj, curObj
@staticmethod def readDB(curObj, sql):
curObj.execute(sql)
result = curObj.fetchall() # 提取查询结果的所有记录
return result
foundList=[] notFouldList=[] class Student: def init(self): self.dbObj, self.curObj = DBOperation.openDB(‘localhost’, ‘root’, ‘root’, 3306, ‘school’)
def searchStu(self,stuList):
for sid in stuList:
sql='select * from student where sid='+str(sid)
result=DBOperation.readDB(self.curObj,sql)
if len(result)==0:
notFouldList.append(sid)
else:
foundList.append(sid)
if name == ‘main‘: list1=[] list2=[] list3=[] list4=[] list5=[] for sid in range(1,31): if sid%5==0: list1.append(sid) elif sid%5==1: list2.append(sid) elif sid%5==2: list3.append(sid) elif sid%5==3: list4.append(sid) elif sid%5==4: list5.append(sid) mylist=[list1,list2,list3,list4,list5] print(list1,list2,list3,list4,list5) thList=[] for i in range(5): th = threading.Thread(target=Student().searchStu,args=(mylist[i],)) thList.append(th)
for th in thList:
th.start()
for th in thList:
th.join()
print(foundList, notFouldList)
```python
import threading
class DBOperation:
@staticmethod
def openDB(host, user, pwd, port, db):
import pymysql
dbObj = pymysql.connect(host=host, user=user, password=pwd, port=int(port), db=db, charset='utf8')
curObj = dbObj.cursor() # 生成游标对象
return dbObj, curObj
@staticmethod
def readDB(curObj, sql):
curObj.execute(sql)
result = curObj.fetchall() # 提取查询结果的所有记录
return result
foundList=[]
notFouldList=[]
class Student:
def __init__(self):
self.dbObj,self.curObj=DBOperation.openDB('localhost','root','root',3306,'school')
def searchStu(self,stuList):
for sid in stuList:
sql='select * from student where sid='+str(sid)
result=DBOperation.readDB(self.curObj,sql)
if len(result)==0:
notFouldList.append(sid)
else:
foundList.append(sid)
if __name__ == '__main__':
list1=[]
list2=[]
list3=[]
list4=[]
list5=[]
for sid in range(1,31):
if sid%5==0:
list1.append(sid)
elif sid%5==1:
list2.append(sid)
elif sid%5==2:
list3.append(sid)
elif sid%5==3:
list4.append(sid)
elif sid%5==4:
list5.append(sid)
print(list1,list2,list3,list4,list5)
th1 = threading.Thread(target=Student().searchStu,args=(list1,))
th2 = threading.Thread(target=Student().searchStu, args=(list2,))
th3 = threading.Thread(target=Student().searchStu, args=(list3,))
th4 = threading.Thread(target=Student().searchStu, args=(list4,))
th5 = threading.Thread(target=Student().searchStu, args=(list5,))
th1.start()
th2.start()
th3.start()
th4.start()
th5.start()
th1.join()
th2.join()
th3.join()
th4.join()
th5.join()
print(foundList, notFouldList)