我有在皇帝模式下运行的wsgi应用程序。问题是在wsgi应用程序中,我启动了一个线程,该线程不与主进程共享数据。
考虑这样的应用程序:
[uwsgi]
module = myproject.myapp:app
uid = www-data
gid = www-data
master = true
processes = 1
socket = 127.0.0.1:8320
chmod-socket = 660
vacuum = true
die-on-term = true
chdir = /etc/wsgiproject/
wsgi-file = /etc/wsgiproject/myproject/myapp.py
python_path = …
enable-threads = true
multithread = true
myapp文件:
from myproject.app import create_app
import threading
app = create_app(port)
if __name__ == "__main__":
app.run(port=port)
def create_app():
app = Flask(__name__)
counter = Counter(app)
counter.number = 10
threading.Thread(name='monitor', target=counter.monitor, daemon=True).start()
return app
class Counter():
def monitor(self):
while(True):
sleep(10)
print(f'current value: self.number')
def __init(self, app):
self.app = app
self.number = 0
app.add_url_rule('/increaseNumber', view_func=self.increase_number, methods=['POST'])
def increase_number(self):
print(f'increased number: {self.number}')
self.number+=1
问题是我的守护进程从不更新数字值,但是在wsgi收到请求时会更新它。
预期输出:
current value: 0
current value: 0
increased number: 1
current value: 1
increased number: 2
current value: 2
实际输出:
current value: 0
current value: 0
increased number: 1
current value: 0 <-- daemon has old value
increased number: 2
current value: 0