Python flask+pymysql 数据库访问异常一次之后就全是异常

2019-01-02 21:17:07 +08:00
 LightingX

跑着跑着莫名其妙,某次请求发出之后就异常了。本来好好的,但是之后就全是这样的异常。

[2019-01-02 21:02:16,374] ERROR in app: Exception on /users [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.5/dist-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.5/dist-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "controller.py", line 31, in get_users
    user_num = dto.count_user()
  File "/root/spider/web/dto.py", line 34, in count_user
    cursor.execute("SELECT COUNT(*) FROM user")
  File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 516, in query
    self._execute_command(COMMAND.COM_QUERY, sql)
  File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 750, in _execute_command
    raise err.InterfaceError("(0, '')")
pymysql.err.InterfaceError: (0, '')

不懂是什么情况。

代码如下: 接口:

@app.route('/user/<uid>')
def get_user_photo(uid):
    uid = int(uid)
    offset = request.args.get('offset', type=int, default=0)
    size = request.args.get('size', type=int, default=8)
    photo_num = dao.count_photo(uid)
    page_info = {}
    if offset+size > photo_num:
        size = photo_num - offset
        page_info = {'has_next': False}
    else:
        page_info = {'has_next': True, 'next': offset+size}
    result_data = dao.get_user_photo_info(uid, offset, size)
    for photo in result_data:
        photo['date'] = str(photo['date'])
    result_data = dict(page_data=result_data)
    result_data['page_info'] = page_info
    result_data['count'] = photo_num
    result_data['user_info'] = dao.get_user_info(uid)
    return filtrate_headers(json.dumps(BaseResponse(200, result_data), default=lambda obj: obj.__dict__))

dao:

def count_user():
    cursor.execute("SELECT COUNT(*) FROM user")
    result = cursor.fetchall()
    return result[0]['COUNT(*)']
 

def get_user_info(uid):
    get_user_sql = "SELECT * FROM user WHERE uid=%d" %uid
    cursor.execute(get_user_sql)
    result = cursor.fetchall()
    if len(result) == 1:
        return result[0]
    else:
        return dict()
1451 次点击
所在节点    问与答
1 条回复
julyclyde
2019-01-03 17:25:36 +08:00
看数据库那边的日志

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/523259

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX