Bu konudaki bilgilere bir ek olarak: Davranışımla biraz karıştım flask.g
, ancak bazı hızlı testler netleştirmeme yardımcı oldu. İşte ne denedim:
from flask import Flask, g
app = Flask(__name__)
with app.app_context():
print('in app context, before first request context')
print('setting g.foo to abc')
g.foo = 'abc'
print('g.foo should be abc, is: {0}'.format(g.foo))
with app.test_request_context():
print('in first request context')
print('g.foo should be abc, is: {0}'.format(g.foo))
print('setting g.foo to xyz')
g.foo = 'xyz'
print('g.foo should be xyz, is: {0}'.format(g.foo))
print('in app context, after first request context')
print('g.foo should be abc, is: {0}'.format(g.foo))
with app.test_request_context():
print('in second request context')
print('g.foo should be abc, is: {0}'.format(g.foo))
print('setting g.foo to pqr')
g.foo = 'pqr'
print('g.foo should be pqr, is: {0}'.format(g.foo))
print('in app context, after second request context')
print('g.foo should be abc, is: {0}'.format(g.foo))
Ve işte verdiği çıktı:
in app context, before first request context
setting g.foo to abc
g.foo should be abc, is: abc
in first request context
g.foo should be abc, is: abc
setting g.foo to xyz
g.foo should be xyz, is: xyz
in app context, after first request context
g.foo should be abc, is: xyz
in second request context
g.foo should be abc, is: xyz
setting g.foo to pqr
g.foo should be pqr, is: pqr
in app context, after second request context
g.foo should be abc, is: pqr
Y4Kman'ın yukarıda söylediği gibi, "Her istek yeni bir uygulama bağlamı getirir". Ve Flask dokümanlarının dediği gibi, uygulama içeriği "istekler arasında paylaşılmayacak". Şimdi, açıkça ifade edilmemiş olan şey (bu ifadelerden ima edildiğine rağmen) ve testimin açıkça gösterdiği şey, hiçbir zaman açıkça bir uygulama bağlamı içinde iç içe birden fazla istek bağlamı oluşturmamanızdır, çünküflask.g
(ve co) • farklı durumların uygulama ve talep seviyelerinde bağımsız olarak mevcut olduğu, bağlamın iki farklı "seviyesinde" işlev gördüğü herhangi bir sihire sahip olmak.
Çünkü gerçeklik, yani "uygulama bağlamı" potansiyel olarak oldukça yanıltıcı adıdır app.app_context()
olan bir istek başına bağlam tamamen aynı, "istek bağlamında" . Bunu, yalnızca normalde bir istek bağlamı gerektiren bazı değişkenlere ihtiyacınız olduğunda gerekli olan ancak herhangi bir istek nesnesine (örneğin bir toplu iş DB kabuk betiği). Uygulama bağlamını birden fazla istek bağlamını kapsayacak şekilde genişletmeye çalışırsanız, sorun istiyorsunuz demektir. Yani, yukarıdaki testim yerine, Flask'ın bağlamlarıyla bunun gibi bir kod yazmalısınız:
from flask import Flask, g
app = Flask(__name__)
with app.app_context():
print('in app context, before first request context')
print('setting g.foo to abc')
g.foo = 'abc'
print('g.foo should be abc, is: {0}'.format(g.foo))
with app.test_request_context():
print('in first request context')
print('g.foo should be None, is: {0}'.format(g.get('foo')))
print('setting g.foo to xyz')
g.foo = 'xyz'
print('g.foo should be xyz, is: {0}'.format(g.foo))
with app.test_request_context():
print('in second request context')
print('g.foo should be None, is: {0}'.format(g.get('foo')))
print('setting g.foo to pqr')
g.foo = 'pqr'
print('g.foo should be pqr, is: {0}'.format(g.foo))
Bu beklenen sonuçları verecektir:
in app context, before first request context
setting g.foo to abc
g.foo should be abc, is: abc
in first request context
g.foo should be None, is: None
setting g.foo to xyz
g.foo should be xyz, is: xyz
in second request context
g.foo should be None, is: None
setting g.foo to pqr
g.foo should be pqr, is: pqr
g
0.10 içinde değiştirmek için bir çeşit istek bağlam nesnesi uygular , aksi takdirde bir sürü kod bazı sapkın hatalar geliştirmeye başlayabilir gibi geliyor.