python - 關(guān)于Flask中全局變量問(wèn)題
問(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...
相關(guān)文章:
1. angular.js - webpack build后的angularjs路由跳轉(zhuǎn)問(wèn)題2. java - web項(xiàng)目中,用戶(hù)登陸信息存儲(chǔ)在session中好 還是cookie中好,取決于什么?3. java - Activity中的成員變量被賦值之后,Activity被回收的時(shí)候內(nèi)存才會(huì)被釋放嗎4. Discuz! Q 有人用過(guò)嗎?5. 數(shù)組按鍵值封裝!6. 我寫(xiě)的哪里有錯(cuò)?請(qǐng)大神幫忙查看一下。7. 請(qǐng)求一個(gè)數(shù)據(jù)返回內(nèi)容為空或者錯(cuò)誤如何再次請(qǐng)求幾次8. 使用list和each配合,的作業(yè),輸出一行后,如何換行9. php由5.3升級(jí)到5.6后,登錄網(wǎng)站,返回的是php代碼,不是登錄界面,各位大神有知道的嗎?10. 為什么bindClass訪問(wèn)不了的?
