我正在尝试编写一个烧瓶扩展,它需要在请求之间保留一些信息。当我使用单个进程运行 Werkzeug 时,这可以正常工作,但是当我使用多个进程运行时,我会遇到一些我不理解的奇怪行为。以这个简单的应用为例:
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)
对于前两种情况,它的行为完全符合我的预期:Counter 对象被初始化一次,然后随着对“/”路由的每个请求而递增。当我使用第三个场景(传递进程=2)运行它时,我得到这个作为输出:
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 -
似乎 counter.value 在初始化后立即返回到它的状态,而实际上没有重新初始化。有人能解释一下 Werkzeug 在内部为实现这一目标所做的工作吗?我也非常有兴趣了解是否有一种方法可以使其行为符合我的天真期望(两个进程,每个进程都有自己的 Counter 实例)。谢谢!