久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

python - 關(guān)于Flask中全局變量問題

瀏覽:153日期:2022-06-29 10:11:22

問題描述

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 None@auth.error_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) } ), 201@app.route(’/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...

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 淳化县| 邵阳市| 广灵县| 翼城县| 措勤县| 长治县| 北安市| 临城县| 南汇区| 黑龙江省| 南康市| 峡江县| 普兰店市| 河池市| 双流县| 七台河市| 北票市| 华坪县| 于田县| 德昌县| 临猗县| 会东县| 徐水县| 济宁市| 宜川县| 南通市| 浪卡子县| 霍林郭勒市| 固原市| 本溪市| 鹰潭市| 茶陵县| 连州市| 海南省| 宜良县| 达孜县| 资阳市| 文昌市| 阜宁县| 冀州市| 景泰县|