av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

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

問(wèn)題描述

Q:看了一個(gè)Flask的簡(jiǎn)單教程,但是有一個(gè)問(wèn)題不明白。Flask中app的全局變量在多個(gè)客戶(hù)端訪問(wèn)的情況下不會(huì)出錯(cuò)嗎?比如下面代碼中tasks列表在多個(gè)客戶(hù)端同時(shí)訪問(wèn)的時(shí)候不會(huì)出錯(cuò)嗎?我用兩個(gè)客戶(hù)端進(jìn)行刪除操作的時(shí)候,沒(méi)有發(fā)現(xiàn)問(wèn)題,但是在并發(fā)更高的情況下會(huì)不會(huì)出現(xiàn)問(wè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)

項(xiàng)目鏈接

問(wèn)題解答

回答1:

http://blog.csdn.net/yueguang...

http://www.cnblogs.com/lqminn...

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 最新中文字幕在线观看 | 黄色大片在线 | 国产精品一级二级三级 | 日韩综合久久 | 久久香蕉精品 | 中文字幕av一区二区三区谷原希美 | 日韩欧美第一页 | 天堂成人在线 | 丨国产丨调教丨91丨 | 亚洲精品免费看 | 国产欧美一区二区精品性色超碰 | 91成人小视频 | 国产高清av| 亚洲成人免费网站 | 亚洲 欧美 激情 另类 校园 | 91成人在线视频 | 欧美精品99 | 黄色片播放 | 欧美激情视频一区二区 | 国产99页 | 久久精品视频免费 | 午夜国产视频 | 黄色小说视频 | 免费观看a级片 | 国产高清视频在线播放 | www国产精品 | 成人毛片100免费观看 | 国产免费黄色片 | 欧美成人精品一区二区三区在线看 | 日韩精品久久久久久 | 亚洲第一天堂网 | 欧美视频二区 | 国产日韩在线视频 | 四虎影院www | 中文字幕伊人 | 亚洲一区网站 | 久久精品视频网 | 五月播播 | 精品一区二区三区免费毛片 | 一区二区免费 | 日本成人中文字幕 |