python - 關(guān)于Flask中全局變量問題
問題描述
Q:看了一個Flask的簡單教程,但是有一個問題不明白。Flask中app的全局變量在多個客戶端訪問的情況下不會出錯嗎?比如下面代碼中tasks列表在多個客戶端同時訪問的時候不會出錯嗎?我用兩個客戶端進行刪除操作的時候,沒有發(fā)現(xiàn)問題,但是在并發(fā)更高的情況下會不會出現(xiàn)問題,表示不理解,求解釋。
PS:下面代碼在python3.0中,需要把task = filter(lambda t: t[’id’] == task_id, tasks)
if len(task) == 0:
改成task = filter(lambda t: t[’id’] == task_id, tasks)task = list(task)
if len(task) == 0:
#!flask/bin/pythonfrom flask import Flask, jsonify, abort, request, make_response, url_forfrom flask.ext.httpauth import HTTPBasicAuthapp = Flask(__name__, static_url_path = '')auth = HTTPBasicAuth()@auth.get_passworddef get_password(username): if username == ’miguel’:return ’python’ return [email protected]_handlerdef unauthorized(): return make_response(jsonify( { ’error’: ’Unauthorized access’ } ), 403) # return 403 instead of 401 to prevent browsers from displaying the default auth dialog @app.errorhandler(400)def not_found(error): return make_response(jsonify( { ’error’: ’Bad request’ } ), 400)@app.errorhandler(404)def not_found(error): return make_response(jsonify( { ’error’: ’Not found’ } ), 404)tasks = [ {’id’: 1,’title’: u’Buy groceries’,’description’: u’Milk, Cheese, Pizza, Fruit, Tylenol’, ’done’: False }, {’id’: 2,’title’: u’Learn Python’,’description’: u’Need to find a good Python tutorial on the web’, ’done’: False }]def make_public_task(task): new_task = {} for field in task:if field == ’id’: new_task[’uri’] = url_for(’get_task’, task_id = task[’id’], _external = True)else: new_task[field] = task[field] return new_task @app.route(’/todo/api/v1.0/tasks’, methods = [’GET’])@auth.login_requireddef get_tasks(): return jsonify( { ’tasks’: map(make_public_task, tasks) } )@app.route(’/todo/api/v1.0/tasks/<int:task_id>’, methods = [’GET’])@auth.login_requireddef get_task(task_id): task = filter(lambda t: t[’id’] == task_id, tasks) if len(task) == 0:abort(404) return jsonify( { ’task’: make_public_task(task[0]) } )@app.route(’/todo/api/v1.0/tasks’, methods = [’POST’])@auth.login_requireddef create_task(): if not request.json or not ’title’ in request.json:abort(400) task = {’id’: tasks[-1][’id’] + 1,’title’: request.json[’title’],’description’: request.json.get(’description’, ''),’done’: False } tasks.append(task) return jsonify( { ’task’: make_public_task(task) } ), [email protected](’/todo/api/v1.0/tasks/<int:task_id>’, methods = [’PUT’])@auth.login_requireddef update_task(task_id): task = filter(lambda t: t[’id’] == task_id, tasks) if len(task) == 0:abort(404) if not request.json:abort(400) if ’title’ in request.json and type(request.json[’title’]) != unicode:abort(400) if ’description’ in request.json and type(request.json[’description’]) is not unicode:abort(400) if ’done’ in request.json and type(request.json[’done’]) is not bool:abort(400) task[0][’title’] = request.json.get(’title’, task[0][’title’]) task[0][’description’] = request.json.get(’description’, task[0][’description’]) task[0][’done’] = request.json.get(’done’, task[0][’done’]) return jsonify( { ’task’: make_public_task(task[0]) } ) @app.route(’/todo/api/v1.0/tasks/<int:task_id>’, methods = [’DELETE’])@auth.login_requireddef delete_task(task_id): task = filter(lambda t: t[’id’] == task_id, tasks) if len(task) == 0:abort(404) tasks.remove(task[0]) return jsonify( { ’result’: True } ) if __name__ == ’__main__’: app.run(debug = True)
項目鏈接
問題解答
回答1:http://blog.csdn.net/yueguang...
http://www.cnblogs.com/lqminn...
相關(guān)文章:
1. android - 安卓做前端,PHP做后臺服務(wù)器 有什么需要注意的?2. docker不顯示端口映射呢?3. mysql - 我用SQL語句 更新 行的時候,發(fā)現(xiàn)全部 中文都被清空了,請問怎么解決?4. python - xpath提取網(wǎng)頁路徑?jīng)]問題,但是缺失內(nèi)容?5. javascript - IOS微信audio標簽不能通過touchend播放6. java中返回一個對象,和輸出對像的值,意義在哪兒7. python-mysql Commands out of sync8. java中這個頁面默認是utf-8編碼的,1輸出亂碼可以理解,可是2就不理解了?9. javascript - 求教各位,本地HTML頁面怎么在DIV中嵌套服務(wù)器上的頁面內(nèi)容?不用iframe。10. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?
