I'm trying to write a flask extension that needs to persist some information between requests. This works fine when I run Werkzeug with a single process but when I run with multiple processes I get some odd behavior that I don't understand. Take this simple application as an example:
from flask import Flask
app = Flask(__name__)
class Counter(object):
def __init__(self, app):
print('initializing a Counter object')
self.app = app
self.value = 0
def increment(self):
self.value += 1
print('Just incremented, current value is ', self.value)
counter = Counter(app)
@app.route('/')
def index():
for i in range(4):
counter.increment()
return 'index'
if __name__ == '__main__':
#scenario 1 - single process
#app.run()
#scenario 2 - threaded
#app.run(threaded=True)
#scenario 3 - two processes
app.run(processes=2)
For the first two scenarios it behaves exactly as I would expect: the Counter object is initialized once and then it increments with every request to the '/' route. When I run it with the third scenario (passing processes=2) then I get this as output:
initializing a Counter object
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Just incremented, current value is 1
Just incremented, current value is 2
Just incremented, current value is 3
Just incremented, current value is 4
127.0.0.1 - - [30/Aug/2015 09:47:25] "GET / HTTP/1.1" 200 -
Just incremented, current value is 1
Just incremented, current value is 2
Just incremented, current value is 3
Just incremented, current value is 4
127.0.0.1 - - [30/Aug/2015 09:47:26] "GET / HTTP/1.1" 200 -
Just incremented, current value is 1
Just incremented, current value is 2
Just incremented, current value is 3
Just incremented, current value is 4
127.0.0.1 - - [30/Aug/2015 09:47:27] "GET / HTTP/1.1" 200 -
It seems that counter.value is returning to it's state right after being initialized without actually being re-initialized. Could somebody shed some light on what Werkzeug is doing internally to make this happen? I'd also be very interested in learning if there is a way to make this behave as I would naively expect (two processes, each with their own instance of Counter). Thanks!