I notice that variables declared in Python get persisted in memory even in new requests. For example:
from flask import Flask
app = Flask(__name__)
basket = ['apple']
@app.route('/cart/', methods=['POST', 'GET'])
def get_cart():
return ', '.join(basket)
@app.route('/store/<string:name>')
def add_item(name):
basket.append(name)
return 'Added ' + name
app.run(port=5000)
I started it by running python app.py
.
At first, the basket
list variable only has the item apple
. After I tried to add another string "orange
" into the basket list, the list now has both apple
and orange
even when I refresh the /cart/
route. The changes made to the basket
list appears to persist even in a new request.
I'd have expected the basket
list variable to be "reset" to its original state in a new request because I have done nothing to persist the changes.
If I happen to have a variable which acts as a flag, a previous request will like alter the value of the flag which affects the next request. The requests then become dependent of each other.
How can I make sure that the changes made by somebody in a previous request don't end up affecting another person's separate request?