【问题标题】:App Engine: debug and run differ (mystery)App Engine:调试和运行不同(神秘)
【发布时间】:2016-01-13 04:42:30
【问题描述】:

我正在构建一个 wiki,但在调试期间的结果似乎与程序运行期间的结果不同时遇到了问题。

class WikiPage(Handler):
    def get(self, id):
            id, existing_article, article_content = self.get_stripped_id_article_content(id)
            logging.error(("GET! id: {0}; article: {1}; content: {2}").format(id, existing_article, article_content))
            <...>

    def strip_id(self, id):
            id = id.replace("/", "")
            return id

    def get_stripped_id_article_content(self, id):
        id = self.strip_id(id)
        q = Article.all()
        q.filter("id = ", id)
        existing_article = q.get()

        content = existing_article.content if existing_article else ""

        return id, existing_article, content
    <...>

class CreateEditPage(WikiPage):
    def post(self, id):
        id, existing_article, article_content = self.get_stripped_id_article_content(id)
        user = self.get_user(self.request)

        input_content = self.request.get("content")

        if existing_article:
            existing_article.content = input_content
            existing_article.put()

        else:            
            new_article = Article(id = id, content = input_content)
            new_article.put()
            id, existing_article, article_content = self.get_stripped_id_article_content(id)
            logging.error(("POST! In Else. id: {0}; article: {1}; content: {2}").format(id, existing_article, article_content))



        id, existing_article, article_content = self.get_stripped_id_article_content(id)
        logging.error(("POST! Outside Else. id: {0}; article: {1}; content: {2}").format(id, existing_article, article_content))
        self.redirect("/" + str(id))

PAGE_RE = r'(/(?:[a-zA-Z0-9_-]+/?)*)'
app = webapp2.WSGIApplication([('/_edit' + PAGE_RE, CreateEditPage),
                               (PAGE_RE, WikiPage),
                               ],
                              debug=True)

首先,我从数据库中清除所有内容并刷新内存缓存。

我运行我的应用程序:

INFO     2015-10-14 21:06:52,744 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO     2015-10-14 21:06:53,135 api_server.py:205] Starting API server at: http://localhost:53588
INFO     2015-10-14 21:06:53,141 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO     2015-10-14 21:06:53,142 admin_server.py:118] Starting admin server at: http://localhost:8000
ERROR    2015-10-14 21:10:10,804 gmv_wiki.py:194] GET! id: ; article: None; content: 
INFO     2015-10-14 21:10:10,810 module.py:809] default: "GET / HTTP/1.1" 302 -
INFO     2015-10-14 21:10:10,847 module.py:809] default: "GET /_edit/ HTTP/1.1" 200 348
ERROR    2015-10-14 21:10:12,228 gmv_wiki.py:194] GET! id: newpost; article: None; content: 
INFO     2015-10-14 21:10:12,239 module.py:809] default: "GET /newpost HTTP/1.1" 302 -
INFO     2015-10-14 21:10:12,264 module.py:809] default: "GET /_edit/newpost HTTP/1.1" 200 348
ERROR    2015-10-14 21:10:18,945 gmv_wiki.py:194] GET! id: new_article; article: None; content: 
INFO     2015-10-14 21:10:18,951 module.py:809] default: "GET /new_article HTTP/1.1" 302 -
INFO     2015-10-14 21:10:18,976 module.py:809] default: "GET /_edit/new_article HTTP/1.1" 200 348
ERROR    2015-10-14 21:10:50,979 gmv_wiki.py:246] POST! In Else. id: new_article; article: None; content: 
ERROR    2015-10-14 21:10:50,985 gmv_wiki.py:251] POST! Outside Else. id: new_article; article: None; content: 
INFO     2015-10-14 21:10:50,991 module.py:809] default: "POST /_edit/new_article HTTP/1.1" 302 -
ERROR    2015-10-14 21:10:51,014 gmv_wiki.py:194] GET! id: new_article; article: None; content: 
INFO     2015-10-14 21:10:51,022 module.py:809] default: "GET /new_article HTTP/1.1" 302 -
INFO     2015-10-14 21:10:51,051 module.py:809] default: "GET /_edit/new_article HTTP/1.1" 200 348
ERROR    2015-10-14 21:11:18,321 gmv_wiki.py:251] POST! Outside Else. id: new_article; article: <gmv_wiki.Article object at 0x7f684d2ba250>; content: New article content.
INFO     2015-10-14 21:11:18,326 module.py:809] default: "POST /_edit/new_article HTTP/1.1" 302 -
ERROR    2015-10-14 21:11:18,351 gmv_wiki.py:194] GET! id: new_article; article: <gmv_wiki.Article object at 0x7f684d2663d0>; content: New article content.
INFO     2015-10-14 21:11:18,358 module.py:809] default: "GET /new_article HTTP/1.1" 200 262

我调试我的应用程序:

pydev debugger: starting (pid: 10230)
INFO     2015-10-14 21:12:24,730 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO     2015-10-14 21:12:25,435 api_server.py:205] Starting API server at: http://localhost:44302
INFO     2015-10-14 21:12:25,463 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO     2015-10-14 21:12:25,467 admin_server.py:118] Starting admin server at: http://localhost:8000
pydev debugger: starting (pid: 10250)
ERROR    2015-10-14 21:12:36,523 gmv_wiki.py:194] GET! id: new_article_1; article: None; content: 
INFO     2015-10-14 21:12:36,547 module.py:809] default: "GET /new_article_1 HTTP/1.1" 302 -
INFO     2015-10-14 21:12:36,766 module.py:809] default: "GET /_edit/new_article_1 HTTP/1.1" 200 348
ERROR    2015-10-14 21:12:54,394 gmv_wiki.py:246] POST! In Else. id: new_article_1; article: None; content: 
ERROR    2015-10-14 21:12:54,430 gmv_wiki.py:251] POST! Outside Else. id: new_article_1; article: None; content: 
INFO     2015-10-14 21:12:54,452 module.py:809] default: "POST /_edit/new_article_1 HTTP/1.1" 302 -
ERROR    2015-10-14 21:12:54,587 gmv_wiki.py:194] GET! id: new_article_1; article: <gmv_wiki.Article object at 0x7fad181bef10>; content: New article 1 content.
INFO     2015-10-14 21:12:54,610 module.py:809] default: "GET /new_article_1 HTTP/1.1" 200 266

您能看看代码中出现 logging.error 的案例吗?以及日志中的结果。

两个绝对相等的情况。 http://localhost:8080/new_article#普通运行 http://localhost:8080/new_article_1#调试

程序的行为如下: 1. 在调试过程中一切正常:创建了一篇新文章的编辑表单,当我按下“提交”时,内容被放置到数据库中,我被重定向到那篇文章的 wiki 页面。 2.当我只是运行程序(不是调试它)时,打开新文章的编辑表单,我输入一些内容并按“提交”。然后我被重定向到 wiki 页面。在该地址找不到 wiki 页面。我再次被重定向到编辑页面。然后当我再次输入内容时,内容最终被放入数据库并打开wiki页面。

对我来说的奥秘是: 1、为什么运行和调试有区别? 2. 为什么我做完new_article.put()后,在post函数中还是没有找到文章对象? 3. 然后我被重定向到 wiki 页面,并且以某种方式找到了文章对象。奇怪的。 4. 为什么existing_article.put() 效果很好,而new_article.put() 没有往数据库里放任何东西?

好吧,我似乎不知所措了。你能帮我解决这些问题吗?谢谢你的建议。

【问题讨论】:

  • 能否请您也发布 WikiPage.get_stripped_id_article_content() 方法的主体?
  • 我编辑了初始文本。请看一下。

标签: python-2.7 google-app-engine


【解决方案1】:

我怀疑您遇到了 GAE 数据存储 eventual consistency 问题。基本上保存在 new_article.put() 中的新文章不会在 Article.all() 查询中找到一段时间(通常很短)。您仍然可以通过 id 访问它,但是您必须为此调整代码。

我怀疑行为上的差异是由于调试模式执行的整体速度较低造成的,这可能会隐藏最终的一致性问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多