目的:让所有的关于数据库的操作都使用线程执行
问题:
1、返回添加成功,但表中没有存在新数据,为什么?
答:因为引用的session并没有包裹住Dept实例方法,所以即使添加数据成功,session并没有执行提交方法
过程:
class DeptHandler(BaseHandler):
"""部门"""
@authenticated_async
async def post(self):
try:
fid = self.get_argument('id')
title = self.get_argument('title')
# ThreadPoolExecutor(1) 开启一个线程 括号中数字几就开几个线程,不传默认开启五个
pool = ThreadPoolExecutor(1)
res = pool.submit(dept_post,fid,title)
self.write(res.result())
except Exception as e:
log.exception('添加部门名称Exception:%s' % e)
def dept_post(fid,title):
try:
with session_maker() as session: