0

PythonでGoogleAppEngineのすべてのDB操作の名前空間を設定しようとしていますが、設定できません。

現在、私のコードは次のようになっています。

""" Set Google namespace """
if user:
    namespace = thisUser.namespace
    namespace_manager.set_namespace(namespace)
""" End Google namespace """

#Then i have all sorts of classes:

class MainPage(BaseHandler):
    def get(self):
        #code with DB operations like get and put...

class MainPage2(BaseHandler):
    def get(self):
        #code with DB operations like get and put...

class MainPage3(BaseHandler):
    def get(self):
        #code with DB operations like get and put...

app = webapp2.WSGIApplication([ ... ], debug=True, config=webapp2_config)

これに伴う問題は、クラスではすべてのDB操作がデフォルトの名前空間で実行されることです(名前空間が設定されていないかのように)。イベントでは、コードの一番上に名前空間を設定しました。

コードの先頭にも設定している変数「namespace」を出力すると、使用したい名前空間が表示されます。

しかし、Google App Engineは、クラスでコードを実行する前に、どこかで名前空間を空にリセットしているようです。

だから今、どこかに名前空間を設定する良い方法があるかどうか疑問に思っています。

現在、私はすべての「def」で次のように設定しています。

class MainPage(BaseHandler):
    def get(self):
        namespace_manager.set_namespace(namespace)

        #code with DB operations like get and put...

class MainPage(BaseHandler):
    def get(self):
        namespace_manager.set_namespace(namespace)

        #code with DB operations like get and put...

etc...

これは、あまりエレガントなソリューションではありません。

4

3 に答える 3

2

リクエストをインターセプトし、アプリのロジックに従って名前空間を設定するミドルウェアを作成する必要があります。

于 2012-07-09T08:50:23.950 に答える
1

良い解決策は、フックを追加することです。そのようなものはうまくいくはずです。

from google.appengine.api import apiproxy_stub_map

NAMESPACE_NAME = 'noname'
def namespace_call(service, call, request, response):
    if hasattr(request, 'set_name_space'):
        request.set_name_space(NAMESPACE_NAME)
apiproxy_stub_map.apiproxy.GetPreCallHooks().Append(
    'datastore-hooks', namespace_call, 'datastore_v3')

main.pyまたはに追加できますappengine_config.py。このようにして、インスタンスのロード中にフックが構成され、その状態が維持されます。

于 2012-07-09T09:29:27.223 に答える
1

appconfig.pyを使用して、namespace_manager_default_namespace_for_request()を定義できます。

https://developers.google.com/appengine/docs/python/multitenancy/multitenancyを読んで、「現在の名前空間の設定」の最初のセクションを参照してください。

于 2012-07-09T09:34:58.443 に答える