亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python - 關于Flask中全局變量問題

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

問題描述

Q:看了一個Flask的簡單教程,但是有一個問題不明白。Flask中app的全局變量在多個客戶端訪問的情況下不會出錯嗎?比如下面代碼中tasks列表在多個客戶端同時訪問的時候不會出錯嗎?我用兩個客戶端進行刪除操作的時候,沒有發現問題,但是在并發更高的情況下會不會出現問題,表示不理解,求解釋。

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...

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 黄色香蕉视频网站 | 婷婷午夜激情 | 114一级毛片免费 | 亚州国产| 在线综合网 | 特级欧美 | 久久伊人中文字幕 | 亚洲国产美女精品久久久久 | 玖玖精品在线观看 | 伊人色综合久久天天爱 | 日韩欧美第一页 | 国产精品宅男在线观看 | 性视频网站视频免费 | 99久久精品免费看国产免费 | 婷婷久久综合网 | 毛片无限看 | 国产精品你懂的在线播放调教 | 国产97在线 | 亚洲 | www.黄色片| 欧美成人免费全网站大片 | 国产成人毛片视频不卡在线 | 久久99精品这里精品3 | 久青草香蕉精品视频在线 | 能看毛片的网址 | 特大一级aaaaa毛片 | 欧美区一区二 | 国产1级片| 国产综合在线观看视频 | 色黄网站aaaaaa级毛片 | 麻豆91制片厂 | 日韩视频网址 | 99国产精品热久久久久久夜夜嗨 | 色男人的天堂 | 日韩精品中文字幕一区三区 | 免费一级a毛片在线播放视 免费一级a毛片免费观看欧美大片 | 国产欧美一区二区三区视频 | 欧美亚洲国产视频 | 日本96在线精品视频免费观看 | 国产精品爱久久电影 | 免费一级生活片 | 国产精品电影久久 |